0

I am trying to send an email to myself using Ajax and PHP. The following seems to be sending the email but doesn't seem to pass the variables into PHP from Javascript.

JavaScript

    var aaa = $('#aaa').val();
    var bbb = $('#bbb').val();
    var data = 'A: ' + aaa + ' B: ' + bbb;

    $.ajax({
    type: "POST",
    url: "sendMail.php",
    data: data,
    success: function(){
    alert("Email Sent");
    }
    });

PHP Code:

<?php
$subject = $_POST['data'];
mail("[email protected]", $subject, "", "From: [email protected]") or die("Error!");
?>

Could anyone please advise on how to fix this?

3
  • 2
    You need to send key-value pairs, not a string (unless you make it a real, encoded query string of course...). Commented Nov 3, 2015 at 21:13
  • "seems to" it either is, or is not. This is science, be precise Commented Nov 3, 2015 at 21:15
  • 1
    your data is invalid. Just give it an object. Keep it simple. Let jQuery do the heavy lifting for you. Commented Nov 3, 2015 at 21:16

2 Answers 2

2

As pointed out in the comments your data variable in js is formatted the wrong way (it needs to be an object! ), you can use this one liner after defining data to convert it into the right format, as data:

data = { data: data };

this would make you not have to adjust the PHP code and populate the 'data' index in your $_POST superglobal with the string.

Sign up to request clarification or add additional context in comments.

4 Comments

Why does OP not want to send this as data: { A: aaa, B: bbb}. I feel like you made this more complicated somehow. I think your declaration statement should read. data = { A:aaa, B:bbb } and his $_POST superglobal will have $_POST['A'] and $_POST['B'], wont it?
Because what he puts into the data field goes to the $subject and actually is supposed to be used as the subject of the mail. This really just is a string. And yes it will be exactly what you describe but then he'll have to adjust the PHP logic also.
Okay I understand now. They should probably do data = { subject: data } then and use $_POST['subject'] if he wants it to be more readable. Atleast that is what I would do.
Jup better naming might be well worth the effort here :)
0
    $to="[email protected]";
    $subject="Work Done by ";
    $subject .= $myusername;
    $headers = 'From: [email protected]' . "\r\n" .'X-Mailer: PHP/' . phpversion();
    $messages ="test" ;
    $messages .="test1" ;
    $ret = mail($to, $subject, $messages, $headers);

2 Comments

A little explanation would be helpful. What is the problem and what is your solution?
Welcome to SO. Code-only answers are not as helpful as answers that have working code plus helpful explanations. In this case, it would be worthwhile explaining why this answer to a 5 year old question is an improvement over the accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.