-1

Im using the following to retrieve a string from php, i would like to know how to make my string into an array.

Jquery

$.get("get.php", function(data){
    alert(data);
    //alert($.parseJSON(data));
}, "json");

the commented out section seems to have no effect, so I cant really tell what I am doing wrong, could someone please advice?

I can post the PHP if needed.

Thanks.

PHP

<?php

$username="root";
$password="root";
$database="testing";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$name= $_GET['name'];

$query="SELECT * FROM tableone ";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$array = array();

$i=0;
while ($i < $num) {

    $first=mysql_result($result,$i,"firstname");
    $last=mysql_result($result,$i,"lastname");
    $date=mysql_result($result,$i,"date");
    $ID=mysql_result($result,$i,"id");

    $array[$i] = $first;

    $i++;
}

echo json_encode($array);

?>

Output:

["James","Lydia","John"]

5
  • 1
    As you have already supplied json as the return dataType data will already be an object you can use, you don't need $.parseJSON(data). This is assuming your PHP page returns data in the correct format. Commented Apr 13, 2012 at 15:42
  • Please post the PHP code that generates the JSON; what you need to to is change the PHP code, not the JavaScript - data is whatever your PHP code returns as JSON. Commented Apr 13, 2012 at 15:42
  • 1
    $.parseJSON turns a string into a JSON object; you've already got a JSON object. Commented Apr 13, 2012 at 15:43
  • possible duplicate of PHP array to jquery array via JSON Commented Apr 13, 2012 at 16:05
  • You do know about mysql_fetch_assoc(), don't you? Commented Apr 13, 2012 at 16:39

2 Answers 2

0

You are getting JSON out in that callback already. It doesn't look like it because when you "alert" it gets converted to a string that consists of a comma delimited list of the array elements.

You can see this by doing alert(data instanceof Array); which will spit out "true".

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

1 Comment

Ok, well I am giving php an array, how might I get data to be an array?
0

You're already getting back something as useful than an array. Let's say your php does this:

<?php echo json_encode(array('John', 'Mary', 'Joseph')); ?>

Inside your function where you're alerting the data, you can get access to the elements like this:

alert(console.log(data[0])); //will alert "John"
alert(console.log(data[1])); //will alert "Mary"

If you need to loop through any of the elements, you can do so like this:

$(data).each(function(index) {
    console.log(this.toString());
});​

2 Comments

thats the thing though, alert(console.log(data[0]);); only returns a character, not a full name
Perhaps you're forgetting the array() inside the json encode?

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.