1

I am looking to send the results from the jQuery quiz "jQuizzy," via email.

This is the code that is sending the POST to file named send.php

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

and here is the simple PHP code to send the email.

<?php
$to = "example@example.com";
$subject = "jQuizzy!";

$jsonStr = $_POST["ajax"];
$json = json_decode($jsonStr);

$body = "$json";

mail($to, $subject, $body);
?>

EDIT: Sorry the issue is that I the results are being posted to the send.php page because the email is going through, but the email is plain/empty.

EDIT 2: I didn't even think about checking the php-error logs to which I found out my server didn't have any set up, but after setting them up it seems there are no errors on the php side of things.

  • 2
    What is the exact problem and error you are getting? – Treffynnon Dec 03 '11 at 01:00
  • 1
    i was totally going to vote this up because i thought you were calling jquery "jquizzy" and that would be awesome. sadly, it's really the name of some sort of quiz framework. –  Dec 03 '11 at 01:02
  • When I finish the quiz it is supposed to send the results to send.php and from there I am attempting to send the results via email. I know the results are being posted because the email is sending, but it is a blank email. – David Jenkins Dec 03 '11 at 01:07

2 Answers2

0

The data parameter supposed to be either valid url-encoded data or an object hash. What you passing is a string what looks like it might be JSON. That doesn't make any sense, but it is hard to suggest something since you haven't really said what your problem is.

Ilia G
  • 10,043
  • 2
  • 40
  • 59
0

Try sending the data as an object.

data: {
    ajax: '[' + collate.join(",") + ']'
},
Zoe Edwards
  • 12,999
  • 3
  • 24
  • 43
  • Tried this, but I am still receiving a blank email. – David Jenkins Dec 03 '11 at 01:15
  • In the PHP file, Try `$body = print_r($_POST["ajax"], true)` and tell us what you get. – Zoe Edwards Dec 03 '11 at 01:49
  • This is what was sent in the email: [{questionNumber:\"1\", UserAnswer:\"2\"},{questionNumber:\"2\", UserAnswer:\"2\"},{questionNumber:\"3\", UserAnswer:\"1\"},{questionNumber:\"4\", UserAnswer:\"1\"},{questionNumber:\"5\", UserAnswer:\"2\"}] – David Jenkins Dec 03 '11 at 01:57
  • [`print_r`](http://php.net/manual/en/function.print-r.php) is just for a human-readabe printout of a variable. Try your suggestion, then if it’s sending you an array, then the easiest thing to do is `$body = implode(",", $json);`. – Zoe Edwards Dec 03 '11 at 02:01
  • That is returning a blank email again – David Jenkins Dec 03 '11 at 02:19
  • Try loading send.php in the browser, without AJAX, but create a sample of the JSON you’re sending, so `$jsonStr = '[...]';` to replace `$_POST['ajax']`. Then use `var_dump($json)` to see what you’ve got. – Zoe Edwards Dec 03 '11 at 10:46
  • $jsonStr = '{"questionNumber":1, "UserAnswer":2}'; '$body = var_dump(json_decode($jsonStr, true)); **output this** array(2) { ["questionNumber"]=> int(1) ["UserAnswer"]=> int(2) }' – David Jenkins Dec 03 '11 at 13:52