How to test PHP form for proper input
am 03.08.2007 20:49:15 von Phil MattI've got a mail form that automatically pulls in the addressee from a MySQL db, and lets the sender
fill in his/her own info before sending. I figured out how to set up tests for legal input in the
different fields, but I don't know how to incorporate the test results into my form submission code.
(Sorry for the dumb question, but I'm not an experienced PHP coder...)
Here the basic stuff:
//SENDS MAIL
if ($submit){
mail($recipient, $subject, $message, "From:\"$from_name\"<$from_email>\r\n" .
"Reply-to: $from_email\r\n" .
"X-Mailer: PHP/" . phpversion());
echo "
Your message has been sent!
";}?>
//FORM USED
//TESTING CODE
$pattern = '/.*@.*\..*/';
extract($_POST);
/*validate*/
function check_from($from_name)
{
if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\]+$/s",$from_na me))
return TRUE;
else
return FALSE;
}
function check_email($from_email)
{
if (!preg_match($pattern,$from_email))
return TRUE;
else
return FALSE;
}
function check_subject($subject)
{
if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\]+$/s",$from_na me))
return TRUE;
else
return FALSE;
}
function check_message($message)
{
if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\]+$/s",$message ))
return TRUE;
else
return FALSE;
}
/*test fails*/
$error=0; // check up variable
/*start*/
if(!check_from($from_name))
{
echo "You haven't entered your name in the Name box!";
$error++; // $error=$error+1;
}
if(!check_email($from_email))
{
echo "You haven't entered a valid email address in the email box!";
$error++;
if(!check_subject($subject))
{
echo "You haven't entered your name in the Name box!";
$error++;
if(!check_message($message))
{
echo "You haven't entered anything in the Message box!";
$error++;
}
if($error==0)
{echo
"Thank you for your email. You should receive a reply shortly from the staff person you contacted";
} else
{
echo"Number of errors: $error";
}
?>
TIA for your help!
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php