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 = "myemail@email.com";
$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?

Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
  • this link will help you to find out the solution : http://stackoverflow.com/questions/2237601/how-to-get-the-post-values-from-serializearray-in-php – jogesh_pi Feb 07 '12 at 12:30
  • I really wish it did! I just don't get it! Maybe I've been staring at this for too long! – dieseljohn Feb 07 '12 at 15:44

5 Answers5

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.
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • 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. – dieseljohn Feb 07 '12 at 14:19
0
$jsonData = file_get_contents('php://input');
$json = json_decode($jsonData, 1);
mail('email', 'subject', print_r($json, 1));
Anthon
  • 69,918
  • 32
  • 186
  • 246
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);
Jochem
  • 2,995
  • 16
  • 18
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.

PHP Connect
  • 539
  • 2
  • 7
  • 24
  • 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. – Jochem Feb 07 '12 at 12:35
  • no i was saying that do not directly do this $body = "$json"; because $json is object not string. – PHP Connect Feb 07 '12 at 16:34
  • In that you are correct! My point was that calling this 'very bad' is a little exaggerated. – Jochem Feb 08 '12 at 08:43
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.

jwchang
  • 10,584
  • 15
  • 58
  • 89