简体   繁体   中英

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. 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:

$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.

 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).

 $.get('mail.php'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM