Page 1 of 1

php validation

Posted: Tue Oct 31, 2006 2:27 pm
by murlopaz
Hi. Do you guys know what is the normal way of validation a form through php. I need the basic method of doing that.

Supposed I have a form with name, last name, email, address etc... If the user doesn't enter his/her name at all i want something in red to be written next to the address or whatever field doesn't contain anything.

Moreover the actual form sends data to some other php file.

Thanks!

Posted: Tue Oct 31, 2006 3:19 pm
by sh33p1985
there is probably a better way to do this, but your script can always catch the empty field using the empty() function and throw the error back to the form.

script:

Code: Select all

$firstName = $_POST['firstName'];
if(empty($firstName)){
header ("location: form.php?emptyfield=firstName");
exit;
}
and in form.php where you want to display the error put:

Code: Select all

if(isset($_GET['emptyfield']) && $_GET['emptyfield'] == "firstName"){
//output error!
}
you could do this check for all your fields but would result in alot of code. i tend to just have one check for all the fields and return a general error.