PHP告诉我填写所有字段

PHP告诉我填写所有字段,php,Php,好的,我有一张联系表: <?php $body = "From: $name\n E-Mail: $email\n Message:\n $message"; if ($_POST['submit']) { if ($name != '' && $email != '') { if ($human == '4') { if (mail ($to, $subject, $body, $from)

好的,我有一张联系表:

<?php
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
    if ($name != '' && $email != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
                echo '<p>Your message has been sent!</p>';
            } else { 
                echo '<p>Something went wrong, go back and try again!</p>'; 
            } 
       } else if ($_POST['submit'] && $human != '4') {
           echo '<p>You answered the anti-spam question incorrectly!</p>';
       }
   } else {
       echo '<p>You need to fill in all required fields!</p>';
   }
}


?>

以及与之配套的HTML代码:


名称

电子邮件
消息
*什么是2+2?(垃圾邮件保护)
但即使所有字段都填好了,它也会不断地告诉我要填写所有必填字段。
提前感谢。

您需要以以下方式获取通过POST提交的所有变量的值: $email=$\u POST['email']

我已经检查并更正了你的代码。这一切都很好,只是您必须声明POST变量:

<?php
  $body = "From: $name\n E-Mail: $email\n Message:\n $message";
  $name = $_POST['name'];
  $email = $_POST['email'];
  $human = $_POST['human'];
  $from = 'example@example.com';
  if ($_POST['submit']) {
    if ($name != '' && $email != '') {
      if ($human == 4) {                 
        if (mail ($email, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
      } else if ($_POST['submit'] && $human != '4') {
       echo '<p>You answered the anti-spam question incorrectly!</p>';
     }
  } else {
   echo '<p>You need to fill in all required fields!</p>';
 }
}
?> 


您还应该使用isset()和检查表单字段是否已提交!空()函数。

我想您应该使用
$\u POST['email']
,而不是
$email
(与其他变量相同)。警告在
mail()
函数中插入未安全的用户输入会打开脚本,使其被滥用(即发送垃圾邮件)。还整理了indentation@EdHeal我得把它修好,让它成为我的头绪this@JohnConde-谢谢-太晚了,无法启动我的IDE
<?php
  $body = "From: $name\n E-Mail: $email\n Message:\n $message";
  $name = $_POST['name'];
  $email = $_POST['email'];
  $human = $_POST['human'];
  $from = 'example@example.com';
  if ($_POST['submit']) {
    if ($name != '' && $email != '') {
      if ($human == 4) {                 
        if (mail ($email, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
      } else if ($_POST['submit'] && $human != '4') {
       echo '<p>You answered the anti-spam question incorrectly!</p>';
     }
  } else {
   echo '<p>You need to fill in all required fields!</p>';
 }
}
?>