Jason,

You didn't mention Javascript checks.

Personally I really dislike having to wati for a page to reload before finding
out that I've just failed to fill in a field.  The Javascript to do basic
on-page checks is all pretty simple stuff.

Of course this doesn't mean we can skip checking the data again in the php!
Javascript might be switched off, we might have a bug in the Javascript (easy to
do), or we (or someone else) might want to simulate form entry via a url link.
It's also quite likely that there may be checks e.g. checks for duplicate
entries, which cannot be done on a form.

Here's some skeleton code that handles a basic form with both php and javascript
checks, (I've deliberately tried to keep the php/Javascript as similar as
possible).  You can try it at http://www.whiffen.net/simple.php if you want.

*** simple.php***
<SCRIPT LANGUAGE="php">

if (isset($surname))
{
   $errormessage = "";

   if ($surname == "")
   {
      $errormessage = $errormessage . "<br>You must enter a value for surname";
   }

   if ($age <= 0 or $age != floor($age))
   {
      $errormessage = $errormessage . "<br>Age must be a whole number";
   }

   if ($errormessage == "")
   {
// do whatever you have to with the data and maybe finish with
// a redirect to a success page
   }
}

print '
<HTML>
<HEAD>
<SCRIPT LANGUAGE="javascript">
function check()
{
    var message = "";
    if (document.myform.surname.value == "")
    {
        message = message+"\nYou must enter a value for surname";
    }
    if (document.myform.age.value != parseInt(document.myform.age.value))
    {
        message = message+"\nAge must be a whole number";
    }
    if (message != "")
    {
        alert("You have the following errors to correct:"+message);
        return false;
    } else {
        return true;
    }
}
</SCRIPT>
</HEAD>
<BODY>
<H1>MY FORM</H1>
<FORM name=myform method=post onSubmit="return check();">
';
if ($errormessage != "")
{
   print 'You have the following errors to correct'.$errormessage.'<BR>';
}
print '
Surname:
<INPUT TYPE=TEXT NAME="surname" VALUE="'.$surname.'">
<BR>
Age:
<INPUT TYPE=TEXT NAME="age" VALUE="'.$age.'">
<BR>
<INPUT TYPE=SUBMIT>
<BR>
<A HREF="'.$SCRIPT_NAME.'s">Source</A>
</FORM>
</BODY>
</HTML>
';
</SCRIPT>

You'll see that http://www.whiffen.net/simple.php?surname=whiffen&age=23.2 works
perfectly well too.

Personally, I'm not a huge fan of Javascript.  It always seems to be much more
trouble than php for some reason, but it has its place, (as long as you never
rely on it!).

George

Jason Dulberg wrote:

> I am working on some error trapping for several forms on my site. After
> visiting a bunch of websites, I've noticed 2 common methods of displaying
> error messages.
>
> 1. display an error box on a new page and force the user to hit the <back>
> button
>
> 2. display the form again with appropriate error text and pre-filled fields.
>
> I have part of the error on the new page working but I'm running into the
> infamous no contents in the form after going <back>.
>
> There are some useability issues with forcing the user to hit the back
> button -- some just don't want to bother.
>
> Is there a way to display the form w/original contents and error messages
> 'without' having to code the entire form twice? I have about 5 forms with 50
> fields or so each.
>
> What would be the best way to go about redrawing the form with the errors
> shown beside each field?
>
> Any suggestions are greatly appreciated.
>
> __________________
> Jason Dulberg
> Extreme MTB
> http://extreme.nas.net




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to