1

I am currently trying to send a 2d array that i receive from a database (sql, phpmyadmin) with ajax.

My ajax function looks like this: (it is generated in php)

$.ajax({
        type: \"POST\",
        url: \"resultQuizGet.php\",
        data: \"getAnswer=\"+question+\"\", //question is just a int variable
        success: function(msg) {
           alert(\"Data Saved: \" + msg);
           }
 });

my resultQuizGet.php file then look like

 $sql = "SELECT `quiz`.question,`quiz`.rightAnswer 
 FROM `quiz` 
 WHERE `quiz`.quizID=:qID";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(":qID", $_POST['quiz']);
$stmt->execute();
$resultsQuiz = $stmt->fetchAll();
echo ...

What do i have to do now to receive a 2d array instead of just a normal string.

What i would like to the msg variable to be is a 2d array that is equal to $resultsQuiz

2
  • 2
    In the php, if it's an array, use json_encode to pass it as a structured array to your ajax call. Then, in your jQuery, use $.parseJSON() to convert it from the json string to an array. Commented May 16, 2014 at 18:57
  • I was looking at it but i dident make it work. I found out something about json_encode() and JSON.stringify() but couldent make it work properly Commented May 16, 2014 at 18:57

1 Answer 1

1

Modify your php as follows, to output the data as a json formatted string:

$sql = "SELECT `quiz`.question,`quiz`.rightAnswer 
 FROM `quiz` 
 WHERE `quiz`.quizID=:qID";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(":qID", $_POST['quiz']);
$stmt->execute();
$resultsQuiz = $stmt->fetchAll();
echo json_encode($resultsQuiz);

Then, modify your jQuery as follow to parse that structured string back into an array / object:

$.ajax({
        type: "POST",
        url: "resultQuizGet.php",
        data: "getAnswer="+question, //question is just a int variable
        success: function(msg) {
           // Note: if it's not json, this can cause a problem
           var data = $.parseJSON(msg);
           // output to your console so you can see the structure
           console.log(data);
           // Utilize the data somehow
           $.each(data, function(key, arr) {
               // Arr should contain an object / array representing the rows from your php results
               console.log(arr);
               // If your row has a field titled `question`, you can access it one of two ways:
               alert(arr.question); // Object notation
               alert(arr['question']); // Array notation
           });
        }
 });
Sign up to request clarification or add additional context in comments.

2 Comments

i have tried this, but i couldent continiue with how to get the 2d array from here
@RobinAndersson - see my revisions. Funny you asked, because I was editing to show this before your comment came in...

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.