0

I am trying to fill a javascript array beaches3 from a php request dynamically with a httpRequest.

var jqxhr = $.get("http://127.0.0.1/websites/map_with_me.php?region="+region+"&suburb="+suburb+"&lat="+lat+"&lon="+lng, function() {
document.getElementById("thediv").innerHTML = (jqxhr.responseText);
var beaches3 = (jqxhr.responseText);
 })

The Output of map_with_me.php is

 [
     ['Bondi Beach', -33.890542, 151.274856, 4],
     ['Coogee Beach', -33.423036, 151.259052, 5],
     ['Cronulla Beach', -34.028249, 121.157507, 3],
     ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
     ['Maroubra Beach', -33.450198, 151.259302, 1]
];

So I want to fill the beaches3 var with the dynamic positions generated by the map_with_me.php file.

if I replace the whole $.get request with the static beaches variable it works.

How do I pass the dynamic javascript generated by the php file to the javascript array?

map_with_me.php

<?php 
echo "var beaches3 = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.423036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 121.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.450198, 151.259302, 1]
];"
?>

This works:

var jqxhr = $.get("http://127.0.0.1/websites/map_with_me.php?region="+region+"&suburb="+suburb+"&lat="+lat+"&lon="+lng, function() {
document.getElementById("thediv").innerHTML = (jqxhr.responseText);
var beaches3 = [
      ['Bondi Beach', -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.423036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 121.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.450198, 151.259302, 1]
    ];
 })

This Doesnt:

var jqxhr = $.get("http://127.0.0.1/websites/map_with_me.php?region="+region+"&suburb="+suburb+"&lat="+lat+"&lon="+lng, function() {
document.getElementById("thediv").innerHTML = (jqxhr.responseText);
var beaches3 = (jqxhr.responseText);
 })

2 Answers 2

0

If you're sure it's an array, an not json or something else, just doing:

$.get("/websites/map_with_me.php?region="+region+"&suburb="+suburb+"&lat="+lat+"&lon="+lng, function(data) {
    $("#thediv").html(data);
    var beaches3 = data;
});

should suffice.

4
  • Thanks, I am returning a php string "[ ['Bondi Beach', -33.890542, 151.274856, 4], ['Coogee Beach', -33.423036, 151.259052, 5], ['Cronulla Beach', -34.028249, 121.157507, 3], ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], ['Maroubra Beach', -33.450198, 151.259302, 1] ];" Commented Apr 29, 2012 at 1:54
  • Yes, that's arrays inside an array, and to make the beaches3 variable into an array with the same structure and values, the above code should work just fine. Commented Apr 29, 2012 at 2:04
  • Thanks @adeneo, I see what you mean: the php file is an array inside the first array. How can I fix this? Commented Apr 29, 2012 at 2:17
  • Try var beaches3 = data[0]; Commented Apr 29, 2012 at 8:57
0

have you tried:

var beaches3 =  JSON.parse(jqxhr.responseText);

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.