Forum Moderators: coopster
If no name is typed then it gives the desired message "fill your name properly." rather than giving "left the name field blank."
if(isset($_POST['name']))//name field is set
{
$n = $_POST['name'];
if (strlen($n) > 0 && strlen($n) < 31 && preg_match("/^[a-zA-Z'-\s]+$/", $n)) //valid and sql friendly name now in $name
{
$name = trim(mysql_real_escape_string($_POST['name']));
}
else {
// $n is not valid
echo "fill your name properly.";
$fault++;
exit;
}
}
if(empty($_POST['name'])) {
//name not set
echo "left the name field blank.";
$fault++;
exit;
}
Even if you have sent nothing in the field - the field is still sent - just with no value, and hence it is 'set'.
if(strlen($_POST['name']))//name field is set
strlen() returns 0 for something of zero length, and 0 evaluates as 'false', whilst non-zero evaluates as true and passes the if() test.
Finally, you should be using:
}
else
{
to give the 'left the name field blank' response - you don't need to check $_POST[name] again.
Syntax:
if (something)
{
...then do...
}
else
{
...otherwise do...
}
if (strlen($n) > 0 && strlen($n) < 31 && preg_match("/^[a-zA-Z'-\s]+$/", $n)) //valid and sql friendly name now in $name
{
$name = trim(mysql_real_escape_string($_POST['name']));
}
else {
// $n is not valid
echo "fill your name properly.";
Aren't you already defining that if no name is given then it should echo fill your name properly?
You can try this. not tested though. If this doesnt work revert back.
<?
if(isset($_POST['name']))//name field is set
{
$n = $_POST['name'];
if (substr_count($n, " ") < 0) {
echo "Left the field blank";
} elseif (strlen($n) > 0 && strlen($n) < 31 && preg_match("/^[a-zA-Z'-\s]+$/", $n)) //valid and sql friendly name now in $name
{
$name = trim(mysql_real_escape_string($_POST['name']));
}else {
// $n is not valid
echo "fill your name properly.";
$fault++;
exit;
}
}
?>