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);
up vote 4 down vote accepted

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/

  • Thanks. Tried it but I'm only getting null back to the js function :/ – holyredbeard Dec 29 '12 at 12:08
  • I did the same thing in my side it works fine. Please double check the PHP code. – Mp de la Vega Dec 29 '12 at 12:10
  • anyways im suspecting your url, please make sure to correct it base on how you can access the php file with a web browser – Mp de la Vega Dec 29 '12 at 12:11
  • No, it have something to do with the change from $.post to $.ajax. $.post works, $.ajax don't :/ – holyredbeard Dec 29 '12 at 12:23
  • 1
    ok i double checked my code, I forgot to include the data that you will send to the server using the $.ajax method, just revised my answer – Mp de la Vega Dec 29 '12 at 12:29

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);
  • JSON.parse in stead of JSON.stringify – Decent Dabbler Dec 29 '12 at 11:59
  • 1
    @fireeyedboy JSON.stringify it shows result as string in console otherwise as object ,just for how data comes – Arun Killu Dec 29 '12 at 12:21
  • Ah yes, I see. Of course. – Decent Dabbler Dec 29 '12 at 12:23

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.