0
我有一個頁面的網站和聯繫表格不提交或產生一個感謝信。按下提交按鈕後,頁面顯示一個空白的PHP頁面。我知道這已被問了幾次,但我找不到解決我的問題的答案。聯繫表[與Recaptcha]
提交後,表單會消失,並且在整個覆蓋消失之前應該顯示一條感謝消息。我也有一個谷歌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>';
}
變量在PHP代碼中聲明。我只是沒有發佈,因爲我不認爲這是問題。我試過錯誤報告,什麼都沒有發生。 – redfelix