Thanks for taking the time to look, guys. I\'m creating a pretty basic AJAX contact form using jQuery. The email sends, but upon opening the email there is no POST data, so
You are using the wrong parameters name, try:
if($_POST){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['text'];
//send email
mail("j.andrew.sears@gmail.com", "51 Deep comment from" .$email, $message);
}
Your PHP script (external file 'email.php') should look like this:
<?php
if($_POST){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['text'];
//send email
mail("j.andrew.sears@gmail.com", "51 Deep comment from" .$email, $message);
}
?>
You code should be:
<section id="right">
<label for="form_msg">Message</label>
<textarea name="form_msg" id="#msg_text"></textarea>
<input id="submit" class="button" name="submit" type="submit" value="Send">
</section>
Js
var data = {
name: $("#form_name").val(),
email: $("#form_email").val(),
message: $("#msg_text").val()
};
$.ajax({
type: "POST",
url: "email.php",
data: data,
success: function(){
$('.success').fadeIn(1000);
}
});
The PHP:
<?php
if($_POST){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['text'];
//send email
mail("email@gmail.com","My Subject:",$email,$message);
}
?>
There is no need to make a query string. Just put your values in an object and jQuery will take care of the rest for you.
var data = {
name: $("#form_name").val(),
email: $("#form_email").val(),
message: $("#msg_text").val()
};
$.ajax({
type: "POST",
url: "email.php",
data: data,
success: function(){
$('.success').fadeIn(1000);
}
});
Leave your email.php code the same, but replace this JavaScript code:
var name = $("#form_name").val();
var email = $("#form_email").val();
var text = $("#msg_text").val();
var dataString = 'name='+ name + '&email=' + email + '&text=' + text;
$.ajax({
type: "POST",
url: "email.php",
data: dataString,
success: function(){
$('.success').fadeIn(1000);
}
});
with this:
$.ajax({
type: "POST",
url: "email.php",
data: $(form).serialize(),
success: function(){
$('.success').fadeIn(1000);
}
});
So that your form input names match up.
You're using the wrong post parameters:
var dataString = 'name='+ name + '&email=' + email + '&text=' + text;
^^^^-$_POST['name']
^^^^--$_POST['name']
etc....
The javascript/html IDs are irrelevant to the actual POST, especially when you're building your own data string and don't use those same IDs.