[英]Why POST method doesn't work when I add this script?
I'm using PHP Mailer and when I added the following script to my HTML code, the $_POST method doesn't retrieve any value from the input fields.我正在使用 PHP Mailer,当我将以下脚本添加到我的 HTML 代码时,$_POST 方法不会从输入字段中检索任何值。 And when I remove, it works normally.当我删除时,它可以正常工作。
<script>
$(document.forms[0]).submit(function(event) {
event.preventDefault();
$.get('mail.php', function(data) {
document.getElementById("Success").style.visibility = 'visible';
});
});
</script>
I'm just using it to keep the user on the same page after submit and then show a success message.我只是用它来让用户在提交后保持在同一页面上,然后显示成功消息。
At the end of the PHP code I'm using this:在 PHP 代码的最后,我使用了这个:
$mail->Body= $body;
if(!$mail ->send()){
echo "Error";
echo $mail ->ErrorInfo;
} else {
http_response_code(200);
}
?>
The first thing you do is prevent the browser from making the POST request it would have made if you didn't include the script.您要做的第一件事是阻止浏览器发出 POST 请求,如果您不包含该脚本,它会发出该请求。
event.preventDefault();
The second thing you do is make an (ajax) GET request instead (which can't have the body you are trying to read).您要做的第二件事是发出(ajax)GET 请求(不能包含您要读取的正文)。
$.get('mail.php'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.