我创建了一个提交到php页面的AJAX表单。此表单已成功提交,我收到了我的数据库中的电子邮件地址,并且我收到了一封确认电子邮件。
但是,我收到一条警告信息“Error |”,显然它来自于:
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());
}
答案 0 :(得分:1)
您的<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="1234567890-"/>
没有任何参考
event
应该是
.on("click", function () {
然后清除表单。你可以做到
.on("click", function (event) {