dslf46995 2015-01-03 03:05
浏览 17
已采纳

PHP告诉我填写所有字段[关闭]

Okay, so I have a contact form:

<?php
$body = "From: $name
 E-Mail: $email
 Message:
 $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>';
   }
}


?>

And the HTML code to go with it:

<form action="" method="POST"> 
<!--PHP Code above goes here-->

<div>
<label>Name</label>
    <br>
    <input name="name" placeholder="Type Here">
</div>
<div>   
    <label>Email</label>
    <br>
    <input name="email" type="email" placeholder="Type Here">
</div>
<div>
    <label>Message</label>
    <br>
    <textarea name="message" placeholder="Type Here"></textarea>
</div>
<div> 
    <label>*What is 2+2? (Spam protection)</label>
    <br>
    <input name="human" placeholder="Type Here">
</div>

<input id="submit" name="submit" type="submit" value="Submit">
</form>

But even when all of the fields are filled in, it keeps telling me to fill in all the required fields. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doushun1904 2015-01-03 03:22
    关注

    You need to get the values of all the variables that are submitted via POST in the following manner : $email = $_POST['email']

    I have checked and corrected your code. It was all good, except that you had to declare the POST variables :

    <?php
      $body = "From: $name
     E-Mail: $email
     Message:
     $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>';
     }
    }
    ?> 
    

    You should also check if whether the form fields are submitted or not by using isset() and !empty() functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大