user1985512 user1985512 - 2 years ago 83
Javascript Question

How can I return a PHP array to the javascript function that called it

..going to try this another way..

Links to another page are not very useful to me.. I've looked at most of them and have not been able to adapt them to what I need.

I'm not familiar with ajax or jQuery.

I could really use some help with detailed EXAMPLES of code that works with regard to this particular question. thanks in advance..

I have this piece of code called: JSON_generator.php

require_once 'EasyWebFetch.php';

$callback = isset($_GET['callback']) ? $_GET['callback'] : 'mymap.weatherhandler';
$station = isset($_GET['rid']) ? $_GET['rid'] : 'FWS';
$product = isset($_GET['product']) ? $_GET['product'] : 'NCR';
$nframes = isset($_GET['frames']) ? $_GET['frames'] : 10;

if (strlen($product) != 3 || strlen($station) != 3) { exit; }

// fetch directory listing
$wf = new EasyWebFetch;
if (!$wf->get("$product/$station/")) {
print $wf->getErrorMessage();
$page = $wf->getContents();
// echo $page."\n\n";

$size = preg_match_all( "/href=\"({$station}[\d_]+{$product}\.gif)\"/" , $page, $match);
$files = $match[1];

$nframes = min(count($files), $nframes);
$files = array_slice($files, -$nframes);

echo $callback."\n(\n{\ndirectory:\n[\n";
for ($i=0; $i < $nframes; $i++) {
echo "\"ridge/RadarImg/$product/$station/$files[$i]\",\n";

echo "]\n}\n)\n;"


When called from the command line like this...

But what I really need is to have those .gif path/file/names "available" from a javascript function that called the php in the first place.** They need to be accessible as items in an array.

I can't figure out the javascript function call to do that..

.. something like this but obviously not this ..

function Feed (whichproduct, whichsite) {
var filelist = [];
var siteimages = filelist("");
//alert (filelist [1]); .. should return something like "ridge/RadarImg/NCR/JAX/JAX_20130128_2354_NCR.gif"

Obviously, I'm a beginner so complete answers would be much more valuable to me than just code snippets..

Bottom line:

I need a javascript function that can pull each of those .gif names from the php script and bring them back into a javascript "var" that I can use elsewhere in the html file.


edit.. edit removed.. figured it out..

Answer Source

I'll answer using jQuery initially, since that makes things very easy, but I'll update in a moment with the vanilla Javascript implementation.

In your Feed function, make an AJAX call using jQuery's convenient $.ajax method.

function Feed (whichproduct, whichsite) {
        "url": ""
        "type": "GET",
        "dataType": "json",
        "success": function( data, status, xhr ){
            alert( data[1] );

In your PHP, use json_encode on a well-formed PHP array (just push to an array() instead of echoing the urls and commas and such directly).

At the very end, print json_encode( $yourArray ).

Update for Vanilla Javascript:

Rather than copying the (extensive) javascript here, I'll just link to a nice SO answer by @Richard H. Hope that helps if you're not looking at using jQuery!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download