I’m trying to handle a php mailform with ajax. It’s a simple form that sends a message to my e-mail with the users phonenumber etc.
I’m not experienced with php at all but I got it to send me mail using a regular submit and then returning to the index page.
As this does not provide any feedback to the user this isn’t working for me. I’ve found some examples using JQuery. I’ve also seen people using both GET and POST. I’m confused now how I should proceed.
The JQuery version of what I’m trying to achieve would look similar to this I guess.
var dataString = 'name='+ name + '&email=' + email + '&message=' + message;
$.ajax({
type: "POST",
url: "mail.php",
data: dataString,
success: function() {
$('#myForm').html("<div id='response'></div>");
$('#response').html("<h2>Contact Form Submitted!</h2>");
}
});
<?php
$mail = $_POST['email '];
$name = $_POST['name'];
$subject = 'new submit';
$text = $_POST['message'];
$to = “yourmail@domain.com”;
$message =” You received a mail from “.$mail;
$message .=” Text of the message : “.$text;
mail($to, $subject,$message)
?>
Solved it like this: