Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
73
rated 0 times [  78] [ 5]  / answers: 1 / hits: 114109  / 11 Years ago, fri, march 8, 2013, 12:00:00

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 I just get the strings I defined in the PHP script. On my phone's email client, the content of the email literally says 'undefined'. I've tried adding different types of header data to no avail, and a number of variations on the PHP mail() function.



I am more than willing to adopt an easier solution for a simple AJAX form, so thanks in advance for any new approaches.



Here is the form:



   <section id=left>
<label for=form_name>Name</label>
<input name=form_name id=form_name type=text >

<label for=form_email>Email</label>
<input name=form_email id=form_email type=email >
</section>

<section id=right>
<label for=form_msg>Message</label>
<textarea name=form_msg id=form_msg></textarea>
<input id=submit class=button name=submit type=submit value=Send>
</section>

</form>


The jQuery AJAX:



$(function() {
$(#contact .button).click(function() {
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);
}
});

return false;
});
});


The PHP script (external file 'email.php'):



<?php
if($_POST){
$name = $_POST['form_name'];
$email = $_POST['form_email'];
$message = $_POST['form_msg'];

//send email
mail([email protected], This is an email from: .$email, $message);
}
?>

More From » php

 Answers
11

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);
}
});

[#79726] Thursday, March 7, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
herman

Total Points: 110
Total Questions: 90
Total Answers: 108

Location: Bosnia and Herzegovina
Member since Thu, Jun 24, 2021
3 Years ago
;