【问题标题】:How do I make a mail form print an error message if something is not filled out?如果未填写某些内容,如何使邮件表单打印错误消息?
【发布时间】:2014-01-05 20:36:57
【问题描述】:

晚上好, 对于我的网站,我使用的是邮件表格。显然它是说消息已发送,尽管没有填写任何内容。那时我也没有收到电子邮件。 我必须在代码中包含什么,以便在发送之前检查电子邮件、主题和消息是否已填写? 谢谢你的帮助!

     <?php
    if (isset($_REQUEST['email']))
    //if "email" is filled out, send email
          {
          //send email
          $email = $_REQUEST['email'] ;
          $subject = $_REQUEST['subject'] ;
          $message = $_REQUEST['message'] ;
          mail("***.****@gmail.com", $subject,
          $message, "From:" . $email);
          echo "Thank you for using our mail form. We will reply as soon as possible.";
          }
    else
          {
          echo "<form method='post' action='mailform.php'>
          Email: <br> <input name='email' type='text'><br>
          Subject: <br> <input name='subject' type='text'><br>
          Message:<br>
          <textarea name='message' rows='15' cols='40'>
          </textarea><br>
          <input type='submit'>
          </form>";
          }
    ?>

【问题讨论】:

  • 类似if (isset($_REQUEST['email'])) and filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL)){ echo 'form valid';} else { ...show your form ... }

标签: php mail-form


【解决方案1】:
<?php
if (isset($_REQUEST['email']) && isset($_REQUEST['message']) && isset($_REQUEST['subject']))
//if "email" is filled out, send email
      {
      //send email
      $email = $_REQUEST['email'] ;
      $subject = $_REQUEST['subject'] ;
      $message = $_REQUEST['message'] ;
      mail("***.****@gmail.com", $subject,
      $message, "From:" . $email);
      echo "Thank you for using our mail form. We will reply as soon as possible.";
      }
else
      {
      if (isset($_REQUEST['submit']){
          echo 'display error here';
       }
      echo "<form method='post' action='mailform.php'>
      Email: <br> <input name='email' type='text'><br>
      Subject: <br> <input name='subject' type='text'><br>
      Message:<br>
      <textarea name='message' rows='15' cols='40'>
      </textarea><br>
      <input type='submit'>
      </form>";
      }
?>

【讨论】:

    【解决方案2】:

    您需要检查$_REQUEST['email'] != '' 而不是isset($_REQUEST['email']),并对其他变量执行相同操作。
    函数isset() 检查变量是否有任何值,包括变量是否被定义为空。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 2015-03-22
      相关资源
      最近更新 更多