0

I have the following code..

if (config.sendResultsURL !== null) 
{
  console.log("Send Results");
  var collate =[];
  for (r=0;r<userAnswers.length;r++)
  {                 
    collate.push('{"questionNumber'+parseInt(r+1)+ '"' + ': [{"UserAnswer":"'+userAnswers[r]+'", "actualAnswer":"'+answers[r]+'"}]}');
  }
  $.ajax({
    type: 'POST',
    url: config.sendResultsURL,
    data: '[' + collate.join(",") + ']',
    complete: function()
    { 
      console.log("Results sent");
    }
  });
}

Using Firebug I get this from the console.

[{"questionNumber1": [{"UserAnswer":"3", "actualAnswer":"2"}]},{"questionNumber2": [{"UserAnswer":"3", "actualAnswer":"2"}]},{"questionNumber3": [{"UserAnswer":"3", "actualAnswer":"2"}]},{"questionNumber4": [{"UserAnswer":"3", "actualAnswer":"1"}]},{"questionNumber5": [{"UserAnswer":"3", "actualAnswer":"1"}]}]

From here the script sends data to emailData.php which reads...

$json = json_decode($_POST, TRUE);
$body = "$json";
$to = "[email protected]";
$email = 'Diesel John';

$subject = 'Results';
$headers  = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";

// Send the email:
$sendMail = mail($to, $subject, $body, $headers);

Now I do get the email however it is blank.

My question is how do I pass the data to emailData.php and from there access it?

2
  • this link will help you to find out the solution : stackoverflow.com/questions/2237601/… Commented Feb 7, 2012 at 12:30
  • I really wish it did! I just don't get it! Maybe I've been staring at this for too long! Commented Feb 7, 2012 at 15:44

5 Answers 5

2
  1. Create an object that you want to pass to PHP
  2. Use JSON.stringify() to make a JSON string for that object.
  3. Pass it to PHP script using POST or GET and with a name.
  4. Depending on your request capture it from $_GET['name'] OR $_POST['name'].
  5. Apply json_decode in php to get the JSON as native object.

In your case you can just pass userAnswers[r] and answers[r]. Array sequence are preserved.

In for loop use,

collate.push({"UserAnswer":userAnswers[r], "actualAnswer":answers[r]});

In ajax request use,

data: {"data" : JSON.stringify(collate)}

In the PHP end,

 $json = json_decode($_POST['data'], TRUE); // the result will be an array.
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this and the email that was sent contains only '1' but I can see the array in the response tab on the console.
0

json_decode converting string to object. just do bellow code and check the values.

print_r($json) 

Directly assign json object to string this is very bad.

3 Comments

Your suggestion will echo the $json structure. Only with output buffering this can be captured. Also "$json" is not 'very bad' in PHP. $json is not interpreted or anything. When $json is an array/object it is rather pointless (for most purposes), but it is NOT bad.
no i was saying that do not directly do this $body = "$json"; because $json is object not string.
In that you are correct! My point was that calling this 'very bad' is a little exaggerated.
0

If you decode your json, you will have a hash and not a string. If you want to be mailed the same as what you printed on the console, simply do this:

$body = $_POST['data'];

Another option would be to parse json into a php hash and var_dump that:

$json = json_decode($_POST['data'], TRUE);
$body = var_export($json, TRUE);

2 Comments

I tried this however the email that gets sent contains "Null"
Try to combine my answer with that of Shiplu!
0

Using below JavaScript code

var collate =[], key, value;
for (r=0;r<userAnswers.length;r++) {   
  key = questionNumber + parseInt(r+1);              
  value = { "UserAnswer": userAnswers[r], "actualAnswer": answers[r] };
  collate.push({ key : value });
}
$.post( config.sendResultsURL, { data: collate  }, function(data) {
  console.log("Results sent"); 
});

And do this in PHP

$data = json_decode( $_POST['data'], true );

and you will have all your data with array.

Comments

0
$jsonData = file_get_contents('php://input');
$json = json_decode($jsonData, 1);
mail('email', 'subject', print_r($json, 1));

Comments

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.