I'm trying to send a simple form to an email using PHP , but cannot get the actual ajax function to work.
This is the form:
<form id="quick-booking-form" method="post" action="#">
<input type="text" name="mail" id="mail">
<textarea name="message" id="message">
</textarea>
<input type="submit" value="Send the Message" id="SendTheForm">
</form>
This is then sent to a separate Javascript function, which holds the ajax:
$(function() {
//alert("page loaded");
$('#quick-booking-form').submit(function(event) {
event.preventDefault();
alert("Start of function");
var mail = $("#mail").val();
var message = $("#message").val();
alert("Mail: " + mail + " Message: " + message);
$.ajax({
type: "POST",
url:"process.php",
data:{
"mail": mail,
"message": message,
},
dataType: "text"
}).success(function(result){
alert(result);
//next line: end of function
}) ;
//next line: end of ajax
});
//next line: end of script
});
which should send the data to this PHP file (process.php):
$msg = $_POST['message'];
$UserName = $_POST['mail'];
$contact = "Booking Manager";
$reply = "[email protected]";
//Subject is always...
$subject = "A message from ".$UserName;
$from = $reply;
//test data
//$contactDetails = "[email protected]";
// send message
$message = "Message reads: ".$msg;
//$to = "[email protected]";
$to = $_POST['mail'];
//$headers = "From: " . $contact."\r\n"."Reply-To:".$reply;
$headers = "From: " . $contact;
mail($to, $subject, $message, $headers);
echo "Mail Sent.";
What should happen is that the form is submitted. It then gets validated (code omitted) in validate.js . If it passes, the form data is sent to process.php. This in turn sends a notification message to [email protected] .
What currently happens is that the alert("Start of Function") fires, and so does the alert showing the correct values.
And this is as far as it gets. The mail function does not seem to fire, and the last alert does not show either.
event.preventDefault()to the beginning of your handler.</form>tag and the first line of your PHP should be changed from$msg = $_POST['mesage'];to$msg = $_POST['message'];. Still not sure if that's the problem or not..done()although expressed as a alternative method to the success callback is still going to fire regardless cause the AJAX is going to complete and trigger it. and I also agree with Fred-ii- to many errors, this looks like a hack job of code copied and pasted from various sources strung together and a few variables changed. Without regards to how things are actually working/meant to work.