Page 1 of 1

new to PHP...help

Posted: Tue Apr 01, 2003 2:48 pm
by vuha2002
I have form.php containing 2 textfields to input a name and a date, and a submit button (< form action="check.php method="post">....)

I'd like to know how I can display back the form after the check.php found the error of the input which also display the previuos corrected input
ex : user inputed correct the name but incorrected the date, the check.php will forward back to the form and prompt user that he/she inputed the date incorrected...also it still display the corrected name in the field name.

thnaks

Posted: Tue Apr 01, 2003 4:39 pm
by McGruff
I typed some php end tags which aren't appearing in this post - hope you can figure out where : (

You can have a single script which either processes or displays a form something like this:

Code: Select all

IF (!isset($_POST['name']) OR !isset($_POST['date'])) {
    // include the form
} ELSE {
    // process the POST data
}
This would catch empty form fields. If you want to validate the data add some more ORs, eg:

Code: Select all

IF (!isset($_POST['name']) OR !isset($_POST['date']) OR !ctype_alpha($_POST['name'])) ..etc
That's just an example which checks for alphanumeric characters in name - add any kind of check you need.

If you include the form, you can set initial values for the form fields like this:

<input type="text" name="name" value="<?php echo htmlspecialchars($_POST['name']); ?>">

(I'm assuming your form is in html)

If the user input fails a check, any values entered when the form was submitted will be redisplayed; if this is the first visit to the form page, nothing is displayed - POST vars aren't set.

To create an error message, add some code to the form such as:

Code: Select all

<?php
IF (!ctype_alpha($_POST['name'])) {
    echo 'Please enter name again';
}
?>
Again, substitute whatever check you want to make instead of ctype_alpha().

You might want to add a block of code as above for each of the validation checks you have, so that a specific error message is displayed for each failed test. Eg if you check if a name is registered you'd maybe want a 'This name is not registered' message when it fails.

Posted: Tue Apr 01, 2003 6:20 pm
by vuha2002
thanks