...

View Full Version : Form validation



vern
09-24-2004, 01:41 AM
I want to make sure that forms that are being submitted have content. How do we check if a field is empty when it is submitted?

I'm guessing



if (isset($_POST['item']))
{
do this ....
}
else
echo 'error';


What is the correct way of making sure a field is not empty?

litebearer
09-24-2004, 02:08 AM
A note...

this will result in 'Set' being echoed


$var = "";
if (isset($var)) {
echo "set";
} else {
echo "not set";
}


you might also check to see that the variable actually contains something

ie..



$var = $_POST['var'];

if (strlen(trim($var)) <0) {
// the variable is either empty or not set
] else {
// there is something in the variable
}


Lite...

Nightfire
09-24-2004, 02:32 AM
Or, to make it easier for yourself, use the empty() (http://www.php.net/empty) function

if(empty($_POST['item'])){
echo 'Item is empty';
}else{
echo 'Item is filled';
}

vern
09-24-2004, 02:35 AM
Looks good. Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum