php - How do i send a 2d array with ajax? -


i trying send 2d array receive database (sql, phpmyadmin) ajax.

my ajax function looks this: (it generated in php)

$.ajax({         type: \"post\",         url: \"resultquizget.php\",         data: \"getanswer=\"+question+\"\", //question int variable         success: function(msg) {            alert(\"data saved: \" + msg);            }  }); 

my resultquizget.php file like

 $sql = "select `quiz`.question,`quiz`.rightanswer   `quiz`   `quiz`.quizid=:qid"; $stmt = $dbh->prepare($sql); $stmt->bindparam(":qid", $_post['quiz']); $stmt->execute(); $resultsquiz = $stmt->fetchall(); echo ... 

what have receive 2d array instead of normal string.

what msg variable 2d array equal $resultsquiz

modify php follows, output data json formatted string:

$sql = "select `quiz`.question,`quiz`.rightanswer   `quiz`   `quiz`.quizid=:qid"; $stmt = $dbh->prepare($sql); $stmt->bindparam(":qid", $_post['quiz']); $stmt->execute(); $resultsquiz = $stmt->fetchall(); echo json_encode($resultsquiz); 

then, modify jquery follow parse structured string array / object:

$.ajax({         type: "post",         url: "resultquizget.php",         data: "getanswer="+question, //question int variable         success: function(msg) {            // note: if it's not json, can cause problem            var data = $.parsejson(msg);            // output console can see structure            console.log(data);            // utilize data somehow            $.each(data, function(key, arr) {                // arr should contain object / array representing rows php results                console.log(arr);                // if row has field titled `question`, can access 1 of 2 ways:                alert(arr.question); // object notation                alert(arr['question']); // array notation            });         }  }); 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -