How do I pass PHP string via ajax into variable array??? ( I´m a jquery noob..)
Hello,
I´m fiddling with a Google Map script and I´m trying to update the map dynamically using ajax.
On pageload the map initiates with an array of coordinates as markers/locations.
The locations data is defined in JS like this example:
- var locations = [
- ['Bondi Beach', -33.890542, 151.274856, 4],
- ['Coogee Beach', -33.923036, 151.259052, 5],
- ['Cronulla Beach', -34.028249, 151.157507, 3],
- ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
- ['Maroubra Beach', -33.950198, 151.259302, 1]
- ];
Now, how do I use Ajax to receive a new array from PHP and store the output as JS variable array?
Here is my ajax script:
- var interval = 5000; // 1000 = 1 second, 3000 = 3 seconds
- function doAjax() {
- jQuery.ajax({
- type: 'POST',
- url: '/codes/LiveVisitsStats/postlivecounter.php',
- dataType : 'html',
- success: function (data) {
- var arr = data.split('|');
- jQuery('#counterint').html(arr[0]);
- jQuery('#extrainfoscounter').html(arr[1]);
- jQuery('#testdiv').html(arr[2]);
-
- var LocationData2 = arr[2];
-
- },
- complete: function (data) {
- // Schedule the next
- setTimeout(doAjax, interval);
- }
- });
- }
- setTimeout(doAjax, interval);
Her is an example of my PHP
postlivecounter.php
:
- $ayyarutest = ' [ ["Bondi Beach", -33.890542, 151.274856, 4],
- ["Coogee Beach", -33.923036, 151.259052, 5],
- ["Cronulla Beach", -34.028249, 151.157507, 3],
- ["Manly Beach", -33.80010128657071, 151.28747820854187, 2],
- ["Maroubra Beach", -33.950198, 151.259302, 1] ] ';
-
- echo $ayyarutest.' |';