new to PHP...help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vuha2002
Forum Newbie
Posts: 2
Joined: Tue Apr 01, 2003 2:48 pm

new to PHP...help

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Last edited by McGruff on Thu Aug 11, 2005 12:26 pm, edited 1 time in total.
vuha2002
Forum Newbie
Posts: 2
Joined: Tue Apr 01, 2003 2:48 pm

Post by vuha2002 »

thanks
Post Reply