【发布时间】:2014-02-12 12:21:25
【问题描述】:
我的页面上有一个联系表单,现在大部分都可以正常工作,但是我创建了一些验证,如果用户以某种方式绕过文本输入的常规 HTML maxlength 参数,它会对字符串长度进行一些后端检查。
这是我的两个问题:
1) 如果我在字段中输入数字,那么我会自动收到一条消息,说我已经超过了最大限制,这显然不应该被执行。
2)第二个问题是,当我收到那条消息(接收错误)时,它至少应该停止发送邮件,因为有问题,但它还是发送了电子邮件!!
随意去尝试一下,亲自看看:
http://eclipse-developers.com/v2/eclipse-developers.com/#contactUs
这是我的联系表单的 PHP 脚本:
// here we put in an if statement to check against missing variables (empty values)
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['message'])){
$name=$_POST['name'];
$organisation=$_POST['organisation'];
$reference=$_POST['reference'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];
// here we are checking to see if that value anything and not just black.
if (!empty($name) && !empty($email) && !empty($subject) && !empty($message) ){
// this is doing a check for max length, its doing it in php just in case the user
// cheats and bypasses the html check.
if (strlen($name>31) || strlen($lname>31) || strlen($organisation>31) || strlen($email>51) || strlen($subject>31) || strlen($reference>31) || strlen($message>2001)){
echo"sorry, max length for a field has been exceeded. Go back and try again</a><br><br>";
}
$to='skyrocketing132@yahoo.com';
$emailsubject=$subject;
$body=$name."\nCompany Name: ".$organisation."\nRef: ".$reference."\nMessage: ".$message;
$headers= 'From: '.$email;
// mails, if statement so if its true (mail did send)
if (mail($to,$emailsubject,$body,$headers)){
echo'Thanks for contacting us.';
}else{
echo'Sorry, an error occurred. Try again later.';
}
}
} else{
}
【问题讨论】:
标签: php forms validation contact