PHP如果字段为空,则不要发送表单

时间:2017-04-26 09:10:40

标签: php html forms

我的网站上有一个PHP表单设置,包括一个"蜜罐"然而,我们最近发现人们提交的空白表格只包含姓名和电子邮件 - 不知道这些的目的是什么,但它们正在变得越来越频繁,我想阻止这些。

这是我的PHP脚本。我想添加一个" if name,email,message"是空白然后重定向到uh-ho.html。有人可以帮忙吗?

<?php
if($_POST){
$to = 'email@email.com';
$subject = 'Website Enquiry';
$from_name = $_POST['name'];
$from_email = $_POST['email'];
$location = $_POST['location'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$message = "From: $from_name\nEmail: $from_email\nTelephone: $phone\nMessage: $message\n\nThis enquiry form was sent from $location";
$robotest = $_POST['robotest'];

if($robotest)
  $error = header('Location: /index.html');
else{

  if($from_name && $from_email && $message){
    $header = "From: Project Customs";

    if(mail($to, $subject, $message, $header))
      $success = header('Location: /thank-you.html');

    else
      $error = header('Location: /uh-ho.html');
  }

  else
    $error = header('Location: /uh-ho.html');
}

if($error)
  header('Location: /uh-ho.html');
elseif($success)
  header('Location: /thank-you.html');
}
?>

1 个答案:

答案 0 :(得分:1)

我建议首先使用CAPTCHA服务之一(Google captcha)来过滤垃圾邮件,

然后对于代码我建议进行以下编辑:

input { 
  max-width:100%
}
.form-group .btn {
	display:block;
}

注意:

header()函数不返回任何值,因此在代码中使用此函数无效,编辑建议是在PHP中使用重定向的更好方法。

相关问题