0
我試圖調用一個php腳本來發送包含來自聯繫表單信息的郵件。表單驗證通過後,我嘗試調用代碼獲取500內部服務器錯誤。調用php腳本時內部服務器錯誤500
這就是我所說的腳本,它的工作原理。
var xmlHttp = new XMLHttpRequest();
//Check if the validation was passed.
if($("#contact-form .error-message").size() === 0) {
xmlHttp.open("POST", "scripts/send_mail.php");
xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlHttp.send("name="+name.value+"&email="+email.value+"&message="+message.value+"&g-recaptcha-response="+captcha);
}
這是劇本被稱爲
<?php
require_once '../init.php';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$captcha = $_POST['g-recaptcha-response'];
$from = '[email protected]';
$to = '[email protected]';
$subject = 'Message from ' . $name;
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
//Verify the captcha by sending a POST-request to Google's server.
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array('secret' => 'xxx', 'response' => $captcha);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$result = json_decode($result, true);
//If the verification of the captcha was successful.
if($result['success']) {
mail($to, $subject, $body, $from)
echo "Thank you! Your message has been received.";
}
else echo "Failed to submit, please try again.";
有誰知道什麼可能導致此?提前
啓用PHP的error_reporting,並給出精確的錯誤。 – jAC
看看服務器的錯誤日誌瞭解有關500的詳細信息。哦,謝謝你的谷歌密鑰。 –
在整個代碼中添加日誌消息,查看服務器日誌,用硬編碼變量替換該帖子,並在瀏覽器中打開頁面,或者至少說出你已經嘗試了什麼,並得到了什麼結果... – BlunT