0

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

})
0

1 Answer 1

1

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!';}?>",
Sign up to request clarification or add additional context in comments.

1 Comment

thanks it's working. but i need to show the double quotation in output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.