Send array from php to javascript

2019-03-04 14:31发布

问题:

I'm screen scraping a site with a php script which creates an array in the end that I want to send back to the javascript caller function. In the code below I've tried to print it out with 'print_r', which doesn't give me any results at all (?). If I echo out on of the elements (e.g $addresses[1]) the element is shown.

So, why ain't I getting anything out from the php function, and what is really the best way to send back the array to the calling js function?

Thanks a lot in advance!

js:

$.post( 
   "./php/foo.php",
   {
    zipcode: zipcode
   },
  function(data) {
     $('#showData').html(data);
  }
);

php:

$tempAddresses = array();
$addresses = array();

$url = 'http://www.foo.com/addresses/result.jspv?pnr=' . $zipcode;

$html = new simple_html_dom();
$html = file_get_html($url);

foreach($html->find('table tr') as $row) {
    $cell = $row->find('td', 0);

    array_push($tempAddresses, $cell);
}

$tempAddresses = array_unique($tempAddresses);

foreach ($tempAddresses as $address) {
    array_push($addresses, $address);
}

print_r($addresses);

回答1:

You can use JSON to return an array back to the client-side, it can be send by AJAX same what you are doing on your existing code.

Use json_encode() of PHP, this function will make your PHP array into a JSON string and you can use it to send back to your client by using AJAX

In your PHP code(Just for demonstration how it works)

json.php

<?php
$addresses['hello'] = NULL;
 $addresses['hello2'] = NULL;
if($_POST['zipcode'] == '123'){ //your POST data is recieved in a common way
  //sample array
  $addresses['hello'] = 'hi';
  $addresses['hello2'] = 'konnichiwa';
}
else{
   $addresses['hello'] = 'who are you?';
   $addresses['hello2'] = 'dare desu ka';
} 
 echo json_encode($addresses);  
?>

then in your client script(much better you use the Jquery's long AJAX way)

$.ajax({
     url:'http://localhost/json.php',
     type:'post',
     dataType:'json',
     data:{ 
         zipcode: '123' //sample data to send to the server
     }, 
     //the variable 'data' contains the response that can be manipulated  in JS 
     success:function(data) { 
          console.log(data); //it would show the JSON array in your console
          alert(data.hello); //will alert "hi"
     }
});

references

http://api.jquery.com/jQuery.ajax/

http://php.net/manual/en/function.json-encode.php

http://json.org/



回答2:

js should be

$.ajax({
     url:'your url',
     type:'post',
     dataType:'json',
     success:function(data) {
      console.log(JSON.stringify(data));
     }
    });

server

$tempAddresses = array();
$addresses = array();

$url = 'http://www.foo.com/addresses/result.jspv?pnr=' . $zipcode;

$html = new simple_html_dom();
$html = file_get_html($url);

foreach($html->find('table tr') as $row) {
    $cell = $row->find('td', 0);

    array_push($tempAddresses, $cell);
}

$tempAddresses = array_unique($tempAddresses);

foreach ($tempAddresses as $address) {
    $arr_res[] =$address;
}
header('content-type:application/json');
echo json_encode($arr_res);