0

I am trying to get data from an SQL Database through jQuery, Ajax and PHP.

Here is the jQuery Code

$.ajax({
    url: 'OpenQuiz.php',
    data: '',
    dataType:'json',
    success: function(data) {
        var one = data[0];
    }
}

$(document).ajaxComplete(function(event,request,settings) {
    alert("check");
}

Here are the json encode lines at the end of the PHP file called "OpenQuiz.php"

echo json_encode($QuizName);
echo json_encode($options);
echo json_encode($votes);
echo json_encode($row_num);
echo json_encode($percentage);
echo json_encode($TruncPercentage);

Please note: $options, $votes, $Percentage and $TruncPercentage are all two-dimensional arrays. $row_num is an integer. $Quiz_Name is a single-dimensional array.

I find that the jQuery runs fine and the ajax request is called because the alert box for "check" shows up. The problem is that I dont know how to access the variables after they have been transferred. I know that it has something to do with data[0] but I don't really understand what "data[0]" means or what it represents. Basically how to I access the variables that I have sent using json_encode in the PHP file?

1 Answer 1

1

data[0] is the first json_encoded item in the array returned. You shouldn't json_encode everything separately, you should build an array and json_encode this.

$items = array('QuizName'=>$QuizName,'options'=>$options, ... ,'TruncPercentage'=>$TruncPercentage);

echo json_encode($items);

Then you retrieve with:

success: function(data) {
    var qn = data.QuizName,
        opt = data.options;
}

And so on for each item, with data.[whatever], and [whatever] being the name of the item you gave it in the array. I know I originally said to retrieve by index (data[0]), but I prefer to retrieve explicitly so that the code is more maintainable.

As a side note, you can eliminate the datatype:'json' declaration in your ajax call by simply setting the header correctly on the PHP side:

header('Content-type: application/json');

Placing that at the top of the document will force the server to recognize the page as json. Far more consistent and explicit than having jQuery look for it.

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

6 Comments

how do I make an array of variables(that are of different type) in PHP?
Makes sense. So since $QuizName is a two-dimensional array, to access a single value in that array I would have to use data.QuizName[x][y] where x and y define the position of the value in the two-dimensional array?
very possible, to be honest i havent tried with two dimensional arrays, but that sounds like it would work. if not, worst case scenario you can split the array into php variables and have them as different items in the array on the json side, and then plug them into different variables on the jquery side.
also, added a tip regarding json pages with ajax.
These are the changes that I have made. I added $items = array('QuizName'=>$QuizName,'options'=>$options,'votes'=>$votes,'row_num'=>$row_num,'percentage'=>$percentage,'TruncPercentage'=>$TruncPercentage);. I removed all the json_encode lines and added echo json_encode($items);. In the jQuery code I removed var one = data[0]; and added var qn = data.QuizName, rn = data.row_num;. Then in the ajaxComplete function I added alert(qn[1][1]); and alert(rn);. However, even then I am only able to see the alert for "check", I cannot see the alerts for qn[1][1] or the alerts for rn.
|

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.