我将google recaptchas实施到我的PHP邮件程序时遇到了一些问题。
所以这是我的问题,当我点击我的联系表格上提交时。我收到403错误,这是由PHP中的最后一个调用。当上述代码不起作用时调用此方法。所以问题是从PHP文件的开头到最后一个else语句之前的某个地方。我无法发现它有什么问题。另外,我不太熟悉PHP。
这是我的HTML
<form id="ajax-contact" method="post" action="../../mailer.php">
<h1>We Look Forward To Speaking With You!</h1>
<input id="name" name="name" placeholder="Name" required>
<input id="email" name="email" type="email" placeholder="Email" required>
<input id="tel" name="tel" type="tel" placeholder="Phone" required>
<input id="time" name="time" type="text" placeholder="Prefered Time: Morning, Noon, Afternoon, Evening">
<input id="method" name="method" type="text" placeholder="Prefered Method of Contact: Phone, Email, Text, Other">
<textarea id="message" name="message" placeholder="Send us a message!" required></textarea>
<div class="recaptcha-box">
<div class="g-recaptcha" data-sitekey="6LdJgSUTAAAAAI6IJfPhaHh7aMWjVPsL4_B9OHGj"></div>
</div>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
这是我的JS
function sendEmail() {
var form = $('#ajax-contact');
var formMessages = $('#form-messages');
var formData = $(form).serialize();
$(form).submit(function(event) {
event.preventDefault();
});
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
.done(function(response) {
$(formMessages).removeClass('error');
$(formMessages).addClass('success');
$(formMessages).text(response);
$('#name').val('');
$('#email').val('');
$('#tel').val('');
$('#time').val('');
$('#method').val('');
$('#message').val('');
location.assign("http://www.theppcgroup.com/thank-you.html");
})
.fail(function(data) {
$(formMessages).removeClass('success');
$(formMessages).addClass('error');
if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Oops! An error occured and your message could not be sent.');
}
});
}
if ($('#submit').on('click', function() {
sendEmail();
}));
这是我的PHP
<?php
if (isset($_POST['sumbit'])) {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$privatekey = 'Cant give you key, Sorry :)';
$response = file_get_contents($url.'?secret='.$privatekey."$response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);
if(isset($data->success) AND $data->success==true) {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$phone = trim($_POST["tel"]);
$time = trim($_POST["time"]);
$method = trim($_POST["method"]);
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "WebInquiry@ThePPCGroup.Com";
// Set the email subject.
$subject = "New contact from $name";
$msg = "Thank you for contacting ThePPCGroup.\nSomeone will contact you shortly. Please let us know the best time to reach you, and if phone or email is better.\nThePPCGroup\n855-539-4742\nWebInquiry@ThePPCGroup.Com\nWWW.ThePPCGroup.Com";
$headers = 'From: WebInquiry@ThePPCGroup.Com' . "\r\n" .
'Reply-To: WebInquiry@ThePPCGroup.Com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Phone: $phone\n\n";
$email_content .= "Prefered Time: $time\n\n";
$email_content .= "Prefered Method: $method\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
mail($email, "Thank You!", $msg, $headers);
}
}
else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>
答案 0 :(得分:0)
它失败了,因为有拼写错误的字符。
更改
if (isset($_POST['sumbit']))
到
if (isset($_POST['submit']))
除此之外,请在javascript中检查$('#form-messages');
ID名称。与PHP id(textarea)不匹配。
希望这能解决问题。
答案 1 :(得分:0)
有一个错字
$response = file_get_contents($url.'?secret='.$privatekey."$response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
需要
$response = file_get_contents($url.'?secret='.$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
有$而不是&amp;