marky
marky

Reputation: 5068

How to properly build array in PHP and get values in JavaScript/jQuery

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?

Upvotes: 0

Views: 113

Answers (3)

Jails
Jails

Reputation: 410

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
        }
    }
});

Upvotes: 1

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

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');

Upvotes: 0

Techie
Techie

Reputation: 45124

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.

Upvotes: 0

Related Questions