php validation

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
murlopaz
Forum Commoner
Posts: 60
Joined: Wed Oct 11, 2006 5:02 pm
Location: Baltimore, MD, USA

php validation

Post 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!
sh33p1985
Forum Commoner
Posts: 78
Joined: Thu Mar 11, 2004 9:22 am

Post 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.
Post Reply