问题
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.
回答1:
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.
回答2:
Try sending the data as an object.
data: {
ajax: '[' + collate.join(",") + ']'
},
来源:https://stackoverflow.com/questions/8364593/i-need-help-sending-jquizzy-results-via-email