0

I am generating an array $array of dates in an external .php file which have this format 2014-01-10(this is Jan).

What i want to achieve is transfer this array to jquery in order to use in a Ajax success callback function.

When i print_r the array in the .php file i get:

Array ( [0] => 2013-12-10 [1] => 2013-12-10 [2] => 2013-12-11 [3] => 2013-12-12 [4] => 2013-12-12 [5] => 2013-12-12 [6] => 2013-12-16 [7] => 2013-12-16 [8] => 2013-12-16 [9] => 2013-12-16 [10] => 2013-12-17 [11] => 2013-12-17 [12] => 2013-12-17 [13] => 2013-12-17 [14] => 2013-12-18 [15] => 2013-12-18 [16] => 2013-12-18 [17] => 2013-12-19 [18] => 2013-12-19 [19] => 2013-12-20 [20] => 2013-12-20 [21] => 2013-12-20 [22] => 2013-12-20 [23] => 2013-12-20 [24] => 2013-12-20 [25] => 2013-12-23 [26] => 2013-12-23 [27] => 2013-12-27 [28] => 2013-12-27 [29] => 2013-12-27 [30] => 2013-12-27 [31] => 2013-12-27 [32] => 2013-12-27 [33] => 2013-12-30 [34] => 2013-12-30 [35] => 2013-12-30 [36] => 2013-12-30 [37] => 2013-12-31 [38] => 2014-01-03 [39] => 2014-01-05 [40] => 2014-01-07 [41] => 2014-01-07 [42] => 2014-01-08 [43] => 2014-01-08 [44] => 2014-01-08 [45] => 2014-01-08 [46] => 2014-01-08 [47] => 2014-01-08 [48] => 2014-01-08 [49] => 2014-01-08 [50] => 2014-01-09 [51] => 2014-01-09 [52] => 2014-01-09 [53] => 2014-01-09 [54] => 2014-01-09 ) 

What i tried is in the .php file :

$tried = implode(",", $array);    
echo $tried;

And then in my .js file the jQuery :

$(document).ready(function() {    
    $.ajax({
        type: 'POST',
        url: 'php/expire.php',
        data: { expired: true },
        success: function(data) {
            takis = data.split(',');
        }
    });
});

But my problem is when the array from the php file is empty the takis.length is 1 while it should be zero. Is there any other way to transfer the array without imploding like using dataType or something? Or am i doing something wrong?

1
  • 1
    Transfer it as a JSON, it will be easier to parse in javascript Commented Jan 11, 2014 at 14:45

4 Answers 4

2

The approach I use for this kind of coding - is to have php arrange variables into JSON code by this command on php-sided script:

$json=json_encode($yourquery);

and it'll fire off the array to jQuery at callback function, and it'll process the JSON string.

Sign up to request clarification or add additional context in comments.

Comments

2

Try this

echo json_encode($array);

now you should be able to access as a standard Java Script Object in the javascript similar to this

data.0.blah

Comments

2

Try that :

$tried = json_encode($array);

Comments

2

Its very simple in your php file you must have something like this:

$array = Array ( '2013-12-10', '2013-12-10', '2013-12-11', '...' );
$array = Array( 'data' => $array );
echo json_encode($arr);

And your javascript script must be something like this:

$(document).ready(function() {

    $.ajax({
    type: 'POST',
    url: 'php/expire.php',
    data: { expired: true },
    success: function(response) {
        takis = response.data;
        console.log( takis ); // show you an array

    }
    });
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.