Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);
share|improve this question
1  
    
try with echo json_encode($addresses); –  Mahesh.D Dec 29 '12 at 11:49

2 Answers 2

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/

share|improve this answer
    
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. –  Mahan 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 –  Mahan 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 –  Mahan 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);
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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