Reputation: 5
I'm not able to get the value from a json-object in jquery. It throws the error message("Uncaught SyntaxError: Unexpected token H") in developer tool.
How can I get the value from my JSON-Object?
Please let me know.
The PHP-Code: (unedited)
[
{
"qus_type": "choice",
"qus_text": "Which one is if statement?",
"correct_answer": "<?php$t=date("H");if ($t<"20") { echo "Haveagoodday!";}?>",
"choice1": "<?php$t=date("H");if ($t<"20") { echo "Haveagoodday!";} else { echo "Haveagoodnight!";}?>",
"choice2": "<?php$t=date("H");if ($t<"20") { echo "Haveagoodday!";}?>",
"choice3": "<?php$t=date("H");if ($t<"10") { echo "Haveagoodmorning!";} elseif ($t<"20") { echo "Haveagoodday!";} else { echo "Haveagoodnight!";}?>",
"choice4": "",
"choice5": ""
}
]
The Javascript / jQuery-part:
jsonObj = jQuery.parseJSON(ques_json);
window.jQuery(jsonObj).each(function(index, item) {
console.log(item.qus_type);
console.log(item.qus_text);
console.log(item.correct_answer);
console.log(item.choice1);
console.log(item.choice2);
console.log(item.choice3);
console.log(item.choice4);
console.log(item.choice5);
})
Upvotes: 0
Views: 61
Reputation: 3064
Couple of typos -
1- <?php$t=
needs a space between <?php
and $t
- <?php $t=
2 - You are reusing a quotation (") in "<?php$t=date("H");
. You need to use an apostrophe in place of one of the sets. Also, you are declaring a < on a string in your if/else (within quotations), int/numbers don't use quotations so maybe...
"choice3": "<?php$t=date('H');if ($t < 10) { echo 'Haveagoodmorning!';} elseif ($t < 20) { echo 'Haveagoodday!';} else { echo 'Haveagoodnight!';}?>",
Upvotes: 1