0

I have to make a JSON ajax request and have an array in return.

i found this code on multiple other questions (edited to my problem):

var hej[];
function ajax_search(){ 
    $.getJSON('test.php',function(response){
    var data = response.get_response;
    for (i in data){
      hej.push([i,data[i]]);
    }
alert(hej[0]);
}

In the php part, i have a database from which i need the data i have have 5 columns pr. row. I only have one row, and i need all five columns in the array.

$sql = "SELECT * FROM 'table'
   LIMIT 1 OFFSET $i"; //random offset
$result = mysql_query($sql);

$storeArray = Array();
while($row = mysql_fetch_array($result))
{
    $storeArray[0]=$row['column1'];
    $storeArray[1]=$row['column2'];
    $storeArray[2]=$row['column3'];
    $storeArray[3]=$row['column4'];
    $storeArray[4]=$row['column5'];
}
$json = json_encode($storeArray);
echo $json;

I know im doing something wrong, and i am new to programming. I need to be able to access all column values in my javascript after the call and use them in other functions, but i cant get it to return.

I would really appriciate help, with both the javascript and the php if there are errors in either.

6
  • What is the test.php script output? (Just open it in a browser and see what it prints) Commented Aug 27, 2012 at 8:33
  • why don't you just json_encode($row)? Commented Aug 27, 2012 at 8:34
  • @Nemaden How can i access every column after that then? Commented Aug 27, 2012 at 8:35
  • I don't see the output if it's not an empty string :) Commented Aug 27, 2012 at 8:35
  • By it's name, e.g. response.column1 Commented Aug 27, 2012 at 8:35

3 Answers 3

0

It should be:

function ajax_search(){ 
    $.getJSON('test2.php',function(response){
        // response.column1
        // response.column2
        // response.column3

        // for array push:
        var hej = [];
        $.each(response, function(key, val) {
             hej.push(val);
        });
        alert(hej[0]); // it will alert column1
    });

}

and MySQL:

$sql = "SELECT `column1`, `column2`, `column3`, `column4`, `column5` FROM `table` LIMIT 1 OFFSET $i"; //random offset
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$json = json_encode($row);
echo $json;
0
0

Try this:

Javascript Code:

var hej[];
function ajax_search(){
    $.getJSON('test.php',function(response){
        $.each(response.results, function(key, val) {
            hej.push(val);
        });
        alert(hej[0]);
    });
}

PHP Code:

<?php
$sql = "SELECT * FROM 'table' LIMIT 1 OFFSET $i"; //random offset
$result = mysql_query($sql);

$storeArray = array();
while($row = mysql_fetch_array($result)) {
    $storeArray[0]= $row['column1'];
    $storeArray[1]=$row['column2'];
    $storeArray[2]=$row['column3'];
    $storeArray[3]=$row['column4'];
    $storeArray[4]=$row['column5'];
}

$json = json_encode( array('results' => $storeArray ) );
echo $json;
?>

Hope this helps.

0

Why use response.get_response?

var hej[];
function ajax_search(){ 
    $.getJSON('test.php',function(data){
    $.each(data, function (i,item) {
      hej.push([i,item]);
    }
alert(hej[0]);
}

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.