0

I have the same question as: I need help sending jQuizzy results Via email it was never solved on the question, anyone have any thoughts? I tried everything on the link above.

This is my code:

$.ajax({
type: 'POST',
dataType: 'text/json',
url: config.sendResultsURL,
data: '[' + collate.join(",") + ']',
complete: function () {console.log("OH HAI");}
});

$name = $_POST['name'];
$to = "email@gmail.com";
$subject = "Quiz Score";
$header = "Content-Type: text/html\r\nReply-To: $to\r\nFrom: $name <$email>";
$jsonStr = $_POST["ajax"];
$json = json_decode($jsonStr);

$body = "$name scored: $json";

mail($to, $subject, $body, $header);

This is the data that gets sent to the php file:

[{questionNumber:"1", UserAnswer:"1"},{questionNumber:"2", UserAnswer:"1"},{questionNumber:"3", UserAnswer:"1"},{questionNumber:"4", UserAnswer:"1"}]:

Update: followed instructions from an answer and there was a request to see my updated send.php file:

<?php
    $name = $_POST['name'];
    $to = "email@gmail.com";
    $subject = "Quiz Score";
    $header = "Content-Type: text/html\r\nReply-To: $to\r\nFrom: $name <$email>";
    $jsonStr = $_POST["ajax"];
    $json = json_decode($jsonStr);

    $body = "$name scored: ".var_dump($json)."";

    mail($to, $subject, $body, $header);
?>
Community
  • 1
  • 1
Stephen
  • 707
  • 3
  • 13
  • 33

1 Answers1

1

There's a few problems here.

  1. $json = $_POST["ajax"]; should be $jsonStr = $_POST["ajax"];
  2. $json is then an array - you need to access the elements of it to work out the score.
  3. The JSON is malformed - json_decode expects fields to be surrounded by speech marks (") as well as the values.

From what I can see, there's no way to pass through what the correct answers are so you'd have to know them in advance and put them in your PHP script to check against the user submitted ones, but I've never used jQuizzy, so I'm guessing!

n00dle
  • 5,949
  • 2
  • 35
  • 48
  • ianhales, thank you - @1, I forgot to change that back, I was trying to dump the raw json string in the php (I'm a js guy not php haha). @2 I can figure out the score situation if I can at least get the data in the email. @3, I changed it to : [{"questionNumber":"1", "UserAnswer":"1"},{"questionNumber":"2", "UserAnswer":"1"},{"questionNumber":"3", "UserAnswer":"1"},{"questionNumber":"4", "UserAnswer":"1"}] and it's still not working. – Stephen May 24 '13 at 14:28
  • What happens if you do `var_dump($json);` in PHP? You should get an array of objects. – n00dle May 24 '13 at 14:38
  • I changed this line: $body = "$name scored: ".var_dump($json).""; and it still is returning a blank email. Here is a link to the page http://tinyurl.com/of35b9t – Stephen May 24 '13 at 15:26
  • Please could you post the full code of "send.php" in your question? I'm sending it valid ajax via a form, but it's still giving null and it should be showing something... – n00dle May 24 '13 at 15:43
  • Very odd, when I run it locally, I get the array of objects as I would expect. I wonder if you've got a problem with `json_decode` on your server. Please could you make a new script (maybe call it info.php) and put inside it: ``. That'll give you information about the PHP install on your server. What version are you running? – n00dle May 24 '13 at 16:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30585/discussion-between-ianhales-and-stephen) – n00dle May 24 '13 at 16:28
  • could you ever figure out any reason that the emails would send blank? – Stephen May 28 '13 at 14:26
  • You needed to add up the score in your PHP code, accessing each question like so: `$answer = $json[0]->UserAnswer` (replace `0` with the index of the question, i.e. second question, you'd have `$json[1]`). Check the scores, add up the correct ones and put that in your email body instead of that `var_dump`. – n00dle May 29 '13 at 14:57