现在我的网站打开,用户可以选择他们想要订购的商品。当他们想要提交订单时,我只想发送一封电子邮件,但电子邮件没有发送,我的页面只是重新启动。
javascript/AJAX:
<script type="text/javascript">
$(document).ready(
function() {
$('#wholesaleOrderForm').submit(
function() {
if (validateForm()) {
$.post("wholesale_form.php",
$('#wholesaleOrderForm').serialize(),
function(response) {
alert('response = ' + response);
if (response == "success") {
$('#ajax-msg').html("Thanks for subscribing!");
$('#ajax-msg').style.color = "green";
$('#ajax-msg').hide(5000);
} else {
$('#ajax-msg').html("Sorry, there is some error. Please try again.");
$('#ajax-msg').style.color = "red";
$('#ajax-msg').hide(5000);
}
}
);
// prevent postback
return false;
} else {
return false;
}
}
);
}
);
</script>
PHP 代码:
<?php
$to = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a161b001b084b4a423a12150e171b131654191517" rel="noreferrer noopener nofollow">[email protected]</a>";
$subject = 'hello';
$message = "Hello!!";
$headers = 'From: DoNotReply@COMPANY_EMAIL.com';
mail($to, $subject, $message, $headers);
?>
最佳答案
您需要使用 event.preventDefault() 禁用表单的默认操作;
关于php - 单击按钮时不会调用 AJAX?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36211290/