I'm having issues trying to get my contact form to email me all the entered form values. Currently the email I'm receiving is only displaying the "message" value, not the name, phone or email. I've looked at a few tutorials but I need something that doesn't reload the page so I'm giving ajax a go. I'm not strong with php or javascript at all so I'm probably missing something pretty obvious..
The alert that is commented out in the script does work.
This is my script:
$('#myform').on('valid.fndtn.abide', function() {
var Name = $("input#Name").val();
var Email = $("input#Email").val();
var Phone = $("input#Phone").val();
var Message = $("textarea#Message").val();
var dataString = 'name='+ Name + '&email=' + Email + '&phone=' + Phone + '&Message=' + Message;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "contactengine.php",
data: dataString,
success: function() {
$('#contact_form').html("<div id='success-message'></div>");
$('#success-message').html("<h2>Message sent!</h2>")
.append("<p>We will be in touch soon. If you haven't had a reply within 2 working days please check your junk folder and mark us as safe.</p>")
.hide()
.fadeIn(1500, function() {
$('#success-message');
});
}
});
return false;
});
And my php:
<?php
$EmailFrom = "me@myemail.co.nz";
$EmailTo = "me@myemail.co.nz";
$Subject = "New message from website contact form";
$Name = Trim(stripslashes($_POST['Name']));
$Phone = Trim(stripslashes($_POST['Phone']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "
";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "
";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "
";
$Body .= "Message: ";
$Body .= "
";
$Body .= $Message;
$Body .= "
";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>