dieseljohn
dieseljohn

Reputation: 1

Accessing JSON object via PHP

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?

Upvotes: 0

Views: 538

Answers (5)

user2234623
user2234623

Reputation: 1

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

Upvotes: 0

jwchang
jwchang

Reputation: 10864

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.

Upvotes: 0

Jochem
Jochem

Reputation: 2994

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

Upvotes: 0

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57640

  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.

Upvotes: 2

PHP Connect
PHP Connect

Reputation: 549

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.

Upvotes: 0

Related Questions