阿贾克斯&单击“提交”时,PHP联系表单显示空白页面

时间:2014-07-23 09:41:34

标签: php jquery html ajax

我尝试在HTML模板/主题上使用此联系表单,但是当我点击"提交"按钮,它将我重定向到一个空白/白页(有一个空的源代码;地址栏显示它是mail.php页面)我是否有效地完成了名称,电子邮件,消息字段或不。如果我有效地完成了这三个字段,那么它会成功发送电子邮件,但它仍然会重定向到空白页面,如果我没有,它只是重定向到空白的mail.php页面而不显示任何php / validation错误。

我在localhost(xampp)和我的实时服务器上测试了这个结果。

contact.html(jQuery - 页面标题内):

<script >
    $("#contactform").submit(function(e){
      e.preventDefault();
      var name = $("#name").val();
      var email = $("#email").val();
      var message = $("#message").val();
      var dataString = 'name=' + name + '&email=' + email + '&message=' + message;
      function isValidEmail(emailAddress) {
        var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
        return pattern.test(emailAddress);
      };

      if (isValidEmail(email) && (message.length > 10) && (name.length > 1)){
        $.ajax({
        type: "POST",
        url: "mail.php",
        data: dataString,
        success: function(){
          $('.success').fadeIn(1000);
        }
        });
      } else{
        $('.error').fadeIn(1000);
      }

      return false;
    });
</script>

contact.html(联系表格):

<form id="contactform" action="mail.php" method="post">
    <table>
        <tr>
            <td><input type="text" id="name" name="name" placeholder="Your Name" /></td>
        </tr>
        <tr>
            <td><input type="text" id="email" name="email" placeholder="Your Email Address" /></td>
        </tr>
        <tr>
            <td><textarea id="message" placeholder="Your Message" name="message" rows="10" cols="20"></textarea></td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="Send" id="send" class="button" />
                <input type="reset" value="Reset" class="button" />
            </td>
        </tr>
    </table>
    <p class="success" style="display:none">Your message has been sent successfully.</p>
        <p class="error" style="display:none">E-mail must be valid and message must be longer than 100 characters.</p>
</form>

mail.php:

<?php
error_reporting( E_ALL );
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['message']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

  // detect & prevent header injections
  $test = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $test, $val ) ) {
      exit;
    }
  }

  //send email
  mail( "johndoe@gmail.com", "Contact Form: ".$_POST['name'], $_POST['message'], "From:" . $_POST['email'] );

}
?>

3 个答案:

答案 0 :(得分:1)

您在表单中的操作不应该是mail.php。因此,在处理AJAX请求之前提交表单。

答案 1 :(得分:1)

只需删除表单操作,然后在同一页面上执行即可通过ajax

提交
<form id="contactform" method="post">
<table>
    <tr>
        <td><input type="text" id="name" name="name" placeholder="Your Name" /></td>
    </tr>
    <tr>
        <td><input type="text" id="email" name="email" placeholder="Your Email Address" /></td>
    </tr>
    <tr>
        <td><textarea id="message" placeholder="Your Message" name="message" rows="10" cols="20"></textarea></td>
    </tr>
    <tr>
        <td>
            <input type="submit" value="Send" id="send" class="button" />
            <input type="reset" value="Reset" class="button" />
        </td>
    </tr>
</table>
<p class="success" style="display:none">Your message has been sent successfully.</p>
    <p class="error" style="display:none">E-mail must be valid and message must be longer than 100 characters.</p>

在下一个你应该添加这个

<?php if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['message']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

// detect & prevent header injections
 $test = "/(content-type|bcc:|cc:|to:)/i";
   foreach ( $_POST as $key => $val ) {
     if ( preg_match( $test, $val ) ) {
     exit;
    }
  }

 //send email
   mail( "johndoe@gmail.com", "Contact Form: ".$_POST['name'], $_POST['message'], "From:" . $_POST['email'] );

}

答案 2 :(得分:0)

我知道我一定犯了一个愚蠢的错误...... :))删除&#34; mail.php&#34;来自联系表单的操作以及在body标记内移动jQuery部分之后,我以某种方式将收件人电子邮件地址更改为johndoe@gmail.com而不是我的真实电子邮件帐户。纠正后一切都开始起作用了。谢谢Sudhanshu和Stefan。