fomicz
fomicz

Reputation: 1792

What's wrong with my AJAX contact form? (php mail())

I`ve created my first ajax contact using jQuery and PHP form but it doesn't seem to work.

My jQuery code:

var dataString = 'address=' + address + '&title=' + title + '&name=' + name + '&mail=' + mail + '&message=' + message;
jQuery.ajax({
    type: "POST",
    url: "sendmail.php",
    data: dataString,
    success: function () {
        alert(dataString);
        jQuery('#ok').html("<h2>Contact Form Submitted!</h2>")
    }
});
return false;

Comment - I`m using alert to display the data string and it shows exactly my string, additionally this script always succeeds and displays "Contact Form Submitted".

Now the sendmail.php part (I believe something is wrong in here):

// getting variables from form

$emailTo = trim($_POST['address']);
$subject = trim($_POST['title']);;
$name = trim($_POST['name']);
$emailFrom = trim($_POST['mail']);
$message = $_POST['message'];

// prepare email body text

$Body = "You have a message from: ";
$Body .= $name;
$Body .= "\n";
$Body .= "\n";
$Body .= $message;

// send prepared message

$sent = mail($emailTo, $subject, $Body);

//callback for jQuery AJAX

if ($sent){
  echo '';
}
else{}

Any ideas? I know I'm not trimming my $message, I will, but I'm sending just one-word testing mails.

All variables come from HTML form and are registered in jQuery script this way - var address = $("#cmail-address").val(); . Anyways that has nothing to do with my problem since I'm getting good callback.

And by the way my server allows to send messages and other scripts work just fine.

Upvotes: 0

Views: 809

Answers (3)

Alex C
Alex C

Reputation: 17004

this might be a bit of a shot in the dark - but have you tried sending to other email addresses ? PHP send mail uses the headers of the server sending the mesage (apache for example). So when you click "SEND" the mail() function attaches an "x-sender: apache" message.

SOme mail servers don't accept mail from servers that have not been authenticated.

When I'm developing locally - I try sending mail to my gmail and it works, using our company's email gets sent down a black hole.

EDIT - oh sorry - you've said that. Try echoing the $_REQUEST to make sure you're sending all the variables correctly. put a print_r($_REQUEST); die(); in send_mail.php then observe the results.

OH! by the way. switch your

$_POST

to

$_REQUEST

I've had that problem before. Like the down voted answer below, you might not actually be sending POST variables because you're passing the params in with a GET format.

 $emailTo = trim($_REQUEST['address']);
 $subject = trim($_REQUEST['title']);;
 $name = trim($_REQUEST['name']);
 $emailFrom = trim($_REQUEST['mail']);
 $message = $_REQUEST['message'];

also try

 print_r($_REQUEST); die();

to see that it's getting passed in.

EDIT2:

As per your comment, see this image for how you'd use firebug

Upvotes: 0

jon_darkstar
jon_darkstar

Reputation: 16768

Your callback function should have something to do with the output from the PHP file. You are just alerting the same string in your javascript scope.

    success: function(dataOut) {  
        alert(dataOut);    
        jQuery('#ok').html("<h2>Contact Form Submitted!</h2>")
}  

Of course, you need the PHP to echo something. Could be as simple as "sent" or "not sent" until you refine stuff later, or it could be exactly the message you want to display. Also echo SOMETHING before you attempt to send the mail. It could help identify that the problem is with actually sending the mail and not your use of ajax.

As one commenter mentioned - strings like "?a=1&b=2&c=3" might work for POST, but name/value pairs {a: "cat", b: "dog"} is probably the better way to go. Strings like that are GET's style, which is probably why someone else confused their answer.

I'd also recommend jQuery's post function. $.post and $.get are simpler and cover 90% of typical ajax uses (including yours!). $.ajax is pretty much for oddball special cases

$.post("sendmail.php", 
       {address: address, title: title, name: name...}, 
       function(d) {alert(d); $("#ok").html("<h2>" + d + "</h2>");});

These last few things are recommendations, they are not likely the cause of your problem, but might make it easier to find that cause.

What IS it that doesn't work? Your PHP is reached but the email doesn't send at all?

Upvotes: 2

Vadyus
Vadyus

Reputation: 1329

You r using $_POST variables, but send them with GET method..

Upvotes: -1

Related Questions