联系表格[与Recaptcha]

时间:2015-10-06 20:57:31

标签: javascript php jquery forms

我有一个单页网站,联系表单没有提交或发出感谢信息。按下提交按钮后,页面显示一个空白的PHP页面。我知道这已被问了几次,但我找不到解决问题的答案。

提交后,表单将消失,并且在整个叠加层消失之前应显示一条感谢信息。我也在表格上有一个google recaptcha。如果用户跳过重新接收验证并提交,则页面只会刷新(或指向主页)。发生这种情况时,如何才能显示错误消息?我在下面提供了一些Javascript和PHP代码。

如何按上述说明显示感谢信息? 对Javascript和PHP都有感谢信息是多余的吗?

HTML:
<form id="contact_form" action="contactform.php" method="POST" role="form">

使用Javascript:

$("#overlayform .contentform input[type=submit]").on('submit', function(e){ // Not sure if I need this.
            e.preventDefault();
$('#overlayform input, #overlayform textarea').blur();
if ($('#overlayform .ErrorField').length > 0) {
 return false;
} else {
 $('#overlayForm .contentform input[type=submit]').addClass('submitted').val('Sending...'); //After clicking the submit button the button shows Sending
var name = $('#overlay_name').val();
var name= $('#overlay_name2').val();
var email = $('#overlay_email').val();
var telephone = $('#overlay_telephone').val();
var message = $('#overlay_message').val();


$.ajax({
  type: "POST",
  url: 'contactform.php',
  data: {
    name: name,
    telephone: telephone,
    message: message,

  },
  success: function(returnData) {
    if (returnData == 'Error') {
      //alert('Error');
    } else {
      $('#overlayform .contentform h2').text('Thank you...');
      $('#overlayform .contentform h4').text('second message');
      $('#overlayform .contentform , #overlayform .contentform input[type=submit], #overlayform .contentform .form').fadeOut('fast');

      $('#overlayform').delay(3000).fadeOut();
       $(".homecard").fadeIn();


    }
  },
  error: function(returnData) {

  }
});
return false;

} });

PHP:

//Recaptcha privatekey etc.
$resp = recaptcha_check_answer ($privatekey,
                                 $_SERVER["REMOTE_ADDR"],
                                 $_POST["recaptcha_challenge_field"],
                                 $_POST["recaptcha_response_field"]);
 if (!$resp->is_valid) {
   // What happens when the CAPTCHA was entered incorrectly
   die ("The reCAPTCHA wasn't entered correctly. Go back and try it again.") //This never shows
        "(reCAPTCHA said: " . $resp->error . ")");
 } else {
   echo "Thank you."
 }
$name= $_POST['overlay_name'];
$name2= $_POST['overlay_name2'];
$email= $_POST['overlay-email'];
$telephone = $_POST['overlay-telephone'];
$message= $_POST['overlay_message'];
$to='email address';
$subject= "Jancarus Contact Form:" $_POST['myOpt'];
 $body = "
<br>
<p>The following information was submitted through the contact form on your website:</p>
<p><b>Name</b>: $name $name2<br>
 <b>Email</b>: $email<br>
</p>";

$headers = "From: $name $name2 <$email> \r\n";
$headers .= "Reply-To: $email \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>$body</body></html>";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
    if (!$resp->is_valid) {              
        if (mail ($to, $subject, $body, $headers)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; //Echo doesn't seem to work. I validated the input fields in Javascript.
    } 
} else if ($_POST['submit'] && $resp) {
    echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
    echo '<p>You need to fill in all required fields!!</p>';
}

1 个答案:

答案 0 :(得分:1)

首先,我会移除验证码系统,直到其他所有东西都正常运行。

其次,为什么要提交表单并通过ajax发送数据?与上面的评论类似,我至少暂时摆脱了ajax处理程序。

第三,如果您获得白屏,可能是因为您收到了PHP错误。你有错误报告吗?你的php有很多未声明的变量...我假设你在其他地方做了那个但是我不确定。

我建议在线查找联系表单教程,然后再进行操作。