0

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

2 Answers 2

4

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/

5
  • Thanks. Tried it but I'm only getting null back to the js function :/ Commented Dec 29, 2012 at 12:08
  • I did the same thing in my side it works fine. Please double check the PHP code. Commented Dec 29, 2012 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 Commented Dec 29, 2012 at 12:11
  • No, it have something to do with the change from $.post to $.ajax. $.post works, $.ajax don't :/ Commented Dec 29, 2012 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 Commented Dec 29, 2012 at 12:29
1

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);
2
  • JSON.parse in stead of JSON.stringify Commented Dec 29, 2012 at 11:59
  • 1
    @fireeyedboy JSON.stringify it shows result as string in console otherwise as object ,just for how data comes Commented Dec 29, 2012 at 12:21

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.