-1
我創建了一個提交給php頁面的AJAX表單。這張表格已成功提交,我收到了我的數據庫中的電子郵件地址,並且發送了一封確認電子郵件。Ajax表單提交成功但出現提示錯誤
不過,我得到一個警告消息,指出「錯誤|」,所以很明顯它是從這個未來:
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + "|" + errorThrown);
我不清楚爲什麼,如果它工作的錯誤是扔的。另一件事,這種形式是重新加載頁面。我有event.preventDefault();
,所以爲什麼要重新加載頁面?
我很感激任何幫助。
<form action="" method="POST">
<input type="email" id="footer-grid1-newsletter-input" placeholder="Your Email Address">
<input type="submit" id="footer-grid1-newsletter-submit" name="submit">
</form>
$(document).ready(function(){
$("#footer-grid1-newsletter-submit").on("click", function() {
event.preventDefault();
var newsletter_email = $("#footer-grid1-newsletter-input").val();
$.ajax({
url: "newsletterSend.php",
type: "POST",
data: {
"newsletter_email": newsletter_email
},
success: function (data) {
// console.log(data); // data object will return the response when status code is 200
if (data == "Error!") {
alert("Unable to insert email!");
alert(data);
} else {
/*$(".announcement_success").fadeIn();
$(".announcement_success").show();
$('.announcement_success').html('Announcement Successfully Added!');
$('.announcement_success').delay(5000).fadeOut(400);*/
}
},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + "|" + errorThrown);
//console.log("error"); //otherwise error if status code is other than 200.
}
});
});
});
ini_set('display_errors', 1);
error_reporting(E_ALL);
$newsletter_email = $_POST['newsletter_email'];
try {
$con = mysqli_connect("localhost", "", "", "");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = $con->prepare("INSERT INTO newsletter (email, subscribed) VALUES (?, NOW())");
if (false===$stmt) {
die('Newsletter email prepare() failed: ' . htmlspecialchars($con->error));
}
$stmt->bind_param('s', $newsletter_email);
if (false===$stmt) {
die('Newsletter email bind_param() failed: ' . htmlspecialchars($stmt->error));
}
$stmt->execute();
if (false===$stmt) {
die('Newsletter email execute() failed: ' . htmlspecialchars($stmt->error));
}
} catch(Exception $e) {
die($e->getMessage());
}
你失蹤'。對一個'event'( 「點擊」,函數(){...'應該是'。對( 「點擊」,函數(事件){' – roullie
設置'dataType',併發送''相同的類型從php' – guradio
@guradio你究竟是什麼意思? – Becky