1

Im trying to query a database from a php file then return the results (userLOC of each row) to the calling Jquery function.

Calling Jquery:

$.post(url, data, function (data) 
    {
        alert(data);
    })

relavent PHP:

 $sql = "Select * from location";
 $result = mysql_query($sql);
 $stack = array();
 while($row = mysql_fetch_array($result))
  {
     array_push($stack, $row["userLOC"]);
  }
  return $stack;

The problem is that I cant return it correctly, if I "echo $stack" it does not work it says Im trying to convert the array to string. If I "echo $stack[0]" it will give me the first result in the array correctly so I know the values are being stored correctly (as are $stack[1], etc.) but this only sends the one value back. And if I "return $stack" as pictured nothing is alerted because nothing is echoed. How would I go about getting the whole array back so that I could iterate through and use them on the jquery side?

DasBeasto
  • 2,082
  • 5
  • 25
  • 65

7 Answers7

3

In PHP

$foo = array();
echo $foo;

will produce the literal string Array as output.

You need to convert your array into a string. Since you're dealing with a Javascript target, use JSON as the perfect means of doing so:

$foo = array('a', 'b', 'c', 'd', 'e');
echo json_encode($foo);

Then in your JS code:

$.get(...., function (data) {
    alert(data[1]); // spits out 'b'
});

Just remember to tell jquery that you're expecting JSON output.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Thanks that pretty much worked, if I just alert(data) now I see the contents of the entire array i.e "["a","b","c"]" so the data is getting there but if I try to reference one of the elements like alert(data[0]) it gives "[" which is the frist character. – DasBeasto Oct 15 '13 at 21:03
  • then it's being treated as a string, and you need to tell jquery you're getting json back, e.g. `$.get(url, data, function, 'json');`, as per the docs: http://api.jquery.com/jQuery.get/ – Marc B Oct 15 '13 at 21:07
1

You need to wrap (serialize) the data in a way that can be transported to the client via HTTP and used to get the original data. Usual container formats are XML and JSON. JSON is well suited for usage in JavaScript, so use the PHP function json_encode() and echo the result.

Thomas
  • 11,272
  • 2
  • 24
  • 40
1

Youll want to use JSON!

Client-side:

jQuery.post(url, data, function( data ) {
    //Data is already converted from JSON
}, "json");

Server-side:

return json_encode($stack);

Explanation

Hope this helps!

Community
  • 1
  • 1
PhilTrep
  • 1,521
  • 1
  • 10
  • 23
0

echo json_encode($stack); can help you

at jquery side use jQuery.parseJSON()

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
0

You need to convert the array to a JSON object like this : return json_encode($stack);

sdespont
  • 13,915
  • 9
  • 56
  • 97
0

Just convert your array it to a JSON object and return it back to your JavaScript:

return json_encode($stack);
akluth
  • 8,393
  • 5
  • 38
  • 42
0

As others have said, you may wish to wrap the output using something like json:

echo json_encode($stack);

However, if you aren't looking for an output with the complexity of JSON formatting, you could just use implode() to output a comma separated list:

echo implode(',',$stack);
404 Not Found
  • 3,635
  • 2
  • 28
  • 34