faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

11 of 13 people (85%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How do I display an error for a signup form in the same page instead of having user to click return?

Nov 27th, 2008 14:20
healthy man, dman, Justin Dalrymple, david foo,


Well, here's the simplest example that I can think of. You set the 
value of an input field to the variable name you plan to use.. so 
example:
INPUT NAME="username" ...
<input type="text" name="username" value="<? echo ($username); ?>">
Then, you check to see if it's blank. If so; you increase the $errors 
variable by one.
Once the form is checked, you check to see if $errors > 0. If so, you 
present the form again with the error printed on top.
Here's an example of the code;
<!-- FORM.INC -->
Please fill in your username and click "Submit"...
<P>
<FORM ACTION="<? echo ("$PHP_SELF"); ?>" METHOD="POST">
Username:
<BR>
<input type="text" name="username" value="<? echo ($username); ?>">
<BR>
<input type="submit" name="submit" value="Submit">
</FORM>
<!-- FORM.PHP -->
<?
$errors = 0;
IF (!isset($submit))
{
include ("FORM.INC");
}
ELSE
{
$required = Array (
'Username' => $username);
while (list($var,$val) = each($required))
{
IF (empty($val))
{
echo ("Field \"$var\" Left Blank.<br>");
++$errors;
}
}
IF ($errors > 0)
{
echo ("<BR>Please Correct Your Errors Below...<BR>");
include ("FORM.INC");
}
ELSE
{
echo ("Form Submission Successful.");
}
}
?>
To use this, simply copy the portions of code and save them to the 
correct file names (FORM.INC and FORM.PHP). Then, access FORM.PHP on 
your webserver and test it out.
To add more variables, simply add them to your FORM.INC file and add 
the variable information to the '$required' array.
Keep in mind that this will only work with TEXT input. If you plan to 
use SELECTS, you'll need to do something like this to FORM.INC:
<?
$options = Array (
'Option01' => 'value1',
'Option02' => 'value2',
'Option03' => 'value3);
echo ("<SELECT NAME=\"select\">");
while (list($var, $val) = each($options))
{
IF ($select == $val)
{
echo ("<option value=\"$val\" SELECTED>$var</OPTION>");
}
ELSE
{
echo ("<option value=\"$val\">$var</OPTION>");
}
}
?>
And the same goes for RADIO, CHECKBOX, SELECT MULTIPLE, etc.
http://www.healthdothealth.com
http://www.healthxhealth.com
http://www.mesotheliomainmesothelioma.com
http://www.healthfirewall.com
http://www.flathealth.com
http://www.healthbodyguard.com