1

I know this has been asked a million times, but it's just not working for me. I have a PHP script (get_pathway_allowed.php) which outputs the following json format when hardcoded

{"pathway_allowed":"n","comment":"one comment"}{"pathway_allowed":"y","comment":"comment two"}{"pathway_allowed":"n","comment":"comment three"}{"pathway_allowed":"n","comment":"comment four"}

Ajax script:

$('.pathway_button').click(function() { 
alert(caseId + ' ' + currentLevel);
$.ajax({
       type: "POST",
       url: "scripts/get_pathway_allowed.php",
       data: {case_id: caseId, level: currentLevel};
       dataType: "json",
       cache: false,
       success: function(response) {
            alert(response[0].pathway_allowed);

    }
});
});

Now alert(caseId + ' ' + currentLevel); shows the correct initial values of the two variables. But I'm not seeing the alert after success. If I remove dataType: "json", I see the success alert with values undefined (to be expected...).

get_pathway_allowed.php has:

$case = $_POST['case_id'];
$level = $_POST['level'];

$query = "SELECT * FROM pathway WHERE level = '$level' AND case_fk = '$case'";
$result = mysql_query($query, $connection) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
        $caseData = array(
        'pathway_allowed' =>  $row['pathway_allowed'],
        'comment' =>  $row['comment']
        );

print json_encode($caseData);
} 

As I said before, hardcoding:

$case = '10';
$level = '1';

outputs the json data as above OK. What am I doing wrong?

0

3 Answers 3

2

the creation of the JSON-string is wrong, use:

$caseData=array();
while ($row = mysql_fetch_array($result)) {
        $caseData[] = array(
        'pathway_allowed' =>  $row['pathway_allowed'],
        'comment' =>  $row['comment']
        );

} 

print json_encode($caseData);

additionally there is a syntax-error:

data: {case_id: caseId, level: currentLevel};
//------------------------------------------^

the semicolon has to be a comma

Sign up to request clarification or add additional context in comments.

Comments

1

Fix your JSON String:

{ "values": [
    {"pathway_allowed":"n","comment":"one comment"},
    {"pathway_allowed":"y","comment":"comment two"},
    {"pathway_allowed":"n","comment":"comment three"},
    {"pathway_allowed":"n","comment":"comment four"}]
}

Comments

1

Your json is not valid, it should look something like this:

{
    "data": [
        {
            "pathway_allowed": "n",
            "comment": "one comment"
        },
        {
            "pathway_allowed": "y",
            "comment": "comment two"
        },
        {
            "pathway_allowed": "n",
            "comment": "comment three"
        },
        {
            "pathway_allowed": "n",
            "comment": "comment four"
        }
    ]
}

use jsonlint to validate your json.

1 Comment

I don't know php, but print json_encode("{\"data\":[".$caseData."]}"); should fix the outer part, you also would need to add commas at the end of each rotation of the while loop except for the last.

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.