Welcome to WebmasterWorld Guest from 54.146.217.179

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php form with validation

php form with validation

     
6:23 pm on Mar 28, 2008 (gmt 0)

New User

5+ Year Member

joined:Aug 21, 2007
posts: 20
votes: 0


Hi All:

I have a form that submits to a php script to process the output but am having difficulty with the validation piece.

The form works fine, submits to my processing page that stores the form field values in variables and then submits an email. I am stuck with how to check for any empty field values (going through them all first) and then redirect back to the original form, repopulating the form with the values that were entererd and indicating to the user (message) the fields that were not filled out.

I know I can use session variables to store the form data but my mind keeps spinning in circles (like my code these days) on how to let the script know to go back and forth.

Any help would be greatly appreciated.

Thanks!

6:49 pm on Mar 28, 2008 (gmt 0)

Full Member

5+ Year Member

joined:June 1, 2007
posts:201
votes: 0


What code are you using for validation ?
6:54 pm on Mar 28, 2008 (gmt 0)

Full Member

10+ Year Member

joined:June 24, 2005
posts:267
votes: 0


You could use javascript

<?php
if(empty($_POST['field1'])) {
echo '<script language="Javascript">
<!--
alert ("Error! Missing Value for field1.")
history.back();
//-->
</script>';
DIE();
} else {
$field1 = mysql_real_escape_string($_POST['field1']);
}
?>
7:15 pm on Mar 28, 2008 (gmt 0)

New User

5+ Year Member

joined:Aug 21, 2007
posts: 20
votes: 0


Thanks for the replies. I was hoping to use php and check for a field value:

-- If a value, store in a session variable for re-use (to repopulate the field if directed back to the form because of an empty value somewhere else) or
-- If no value, set a message value for that field and store it in a session variable to be passed back to the form for displaying to the user.

Ideally I would like to "test" all field values and generate variables for all fields before going back to tell the user, or in the case of success just executeg my script after the validation.

I can use javascript, but my process form is already in php so figured I could keep it together.