提交后如何通过用户输入发送电子邮件而不离开页面[关闭]

I have page (contact me.php). I would like user to input their enquiry and email and submit.

I only know how to submit to the next page and gives a redirect back to the page.

How do i code in such a way that when user submit, it stays on the same page and display a notification like "Enquiry sent"

thanks many!

Please try to use AJAX. By using AJAX, not need to update whole page.

Put the code to send the email in a file and call $.ajax() if you plan to use jQuery. Simply pass the form data as either POST or GET variables to this file.

You can echo back to the calling page a value for success or error and handle this appropriately in the calling page.

Don't forget to check the variables you pass from the form using filter_var() etc.

You need to Send Email Form using Ajax like:

$.ajax({
  url: 'mail file url',
  type: 'post',
  data: {'action': 'data1': 'value1'},
  success: function(data, status) {
    if(data == "ok") {
      //your success message here 
    }
  },
  error: function(xhr, desc, err) {
    console.log(xhr);
    console.log("Details: " + desc + "
Error:" + err);
  }
}); // end ajax call

Call this function through Ajax you can send email. it will send email without reload.

function sendemail(){

var id=document.getElementById("email_id").value;
var msg=document.getElementById("message").value;

$.ajax({
url: 'mail.php?email_id='+id+"&message='+msg,
type: 'post',
data: {'action': 'data1': 'value1'},
success: function(data, status) {
if(data == "ok") {
  //your success message here 
}
},
error: function(xhr, desc, error) {
console.log("error" + error);
}
});

}

You could use html and jquery however your php would pretty much stay the same. I have included the html and jquery below. you can do the contact us page the way you normally do it.

----------



<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Contact Us</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
    <script>


    $(function() {

      $("#Send").click(function(){
        var name =$("#name").val();
            $.post('contactus.php',{Name:name},function(data)
                        {

                            alert(data);

                            });




        };


   });


    </script>
</head>
<body>  
    <form method="post" onsubmit='return false;' id="form">
        Name: <input type="text" name="name" required id="name"><br>

        <input type="submit" id="Send" value="Send">
    </form>
</body>
</html>

You can send email using php mail function or you can use some other plugin like PHPmailer

And after you send email, you can direct your page in the same page use php syntax header("Location: yourpage.php")

That's what i use

Hope it can help you