0

From a database query I'm getting one record returned with fields/values and putting it into an array:

$arr = array();
while ($rowSearchZipCode = mysqli_fetch_array($resultSearchZipCode, MYSQLI_ASSOC)) {
    $arr[] = $rowSearchZipCode;
}

I'm sending this back to a jQuery ajax call via an echo from the php script:

echo print_r($arr);

Displaying the array in an alert box in the success parameter of $.ajax shows me that I'm getting a multidimensional array that looks like this:

Array
(
[0] => Array
    (
        [id] => 55
        [branchName] => SBN - Some Branch Name
        [branchAddress1] => 1234 Elm St.
        [branchAddress2] => Suite 000
        [branchAddressCity] => Anywhere
        [branchAddressState] => CA
        [branchAddressZip] => 55000
        [branchAddressPhone] => 555-555-5555
        [branchAddressFax] => 555-555-4444
    )

)
1

Question 1: Am I building the array correctly in the while loop?

Question 2: In the $.ajax call how do I get the field/value pairs from the array so that I can output them to HTML tags, etc?

3 Answers 3

1

PHP side :

  • don't forget to send the correct JSON header with:

    header('Content-Type: application/json');
    
  • and use json_encode to encode your array before echoing it:

    echo json_encode($myarray);
    

Client side use :

$.ajax({
    url: '<url>',
    dataType: "json", //optionnal if you correctly set the header
    success: function(data){
        for (var i in data) {
            console.log(data[i]); //data[i] are JS object
        }
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

@Jalis This gave me the array data in the console, so I'm successfully parsing it out in the success function, but since I know the field names (branchAddress1, branchName, etc) can I use those somehow to grab it's associated value from the array?
Sorry, yes it does. One more "but what if": Can I get those values without looping through the array?
data[0]['branchName'], data[1]['branchName'], etc. ? but if you have a single array, better to echo json_encode(reset($myarray)) and use directly data['branchName'] syntax
That relies on knowing the index of the key in question (which in this case I do). Is there a way to get the value when you know the key name, but not it's index? (You may have answered that when I was posting this...)
The reset function did it! Thanks!
0
Question 1: Am I building the array correctly in the while loop?

Yes

Question 2: In the $.ajax call how do I get the field/value pairs from the array so that I can output them to HTML tags, etc?

You can use json_encode before sending the array when the AJAX call is made like below.

echo json_encode($arr);

Use jQuery jQuery.parseJSON() to decode the array in the success function of the AJAX call. Use a preferred loop to loop through the array.

EG :

$.ajax({
  url: myUrl,
  cache: false,
  dataType: "json",
  success: function(data){
    //use jQuery.parseJSON() & foreach goes here.
  },
  error: function(e, xhr){
    //alert('error');
  }
});

JSON (JavaScript Object Notation) is a lightweight data-interchange format. Read more.

Comments

0

Question 1: Am I building the array correctly in the while loop?

yes

Question 2: In the $.ajax call how do I get the field/value pairs from the array so that I can output them to HTML tags, etc?

Do not use print_r, use json_encode. JSON is a subset of JavaScript and JQuery.ajax will give you the ready-to-use JavaScript object as response. To force this, add dataType: 'json' to the AJAX options and send the correct content type header with PHP before echo'ing the JSON:

header('Content-type: application/json');

Comments

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.