2

I had a weird problem here. Please check the code below. I do not understand how this code below is created by somehow part of it is working, considering mixture of php variables and javascript variables.

First, let us see what the alert() outputs (take note that $lat and $lng is different from the array $vlat):

  1. In Line 8 of the code below, alert(eventlocation) properly displays the coordinates of the GoogleMap latlng (so the implementation is correct)

  2. In Line 13, alert(s) was able to display incrementing values (i.e. 0,1,2,3,4,..) based on the for loop in the previous line so it is also correct.

  3. In Line 14, alert($vlat[0]) was able to display the latitude of the first element of that array (declared before this set of codes) so it is also correct

  4. In Line 15, alert($vlat[1]) was able to display the latitude of the second element of that array so it is also correct

  5. But in Line 16, alert($vlat[s]) displayed undefined.

Can anyone explain that? Thanks

    echo "<script src= 'http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAA2jrOUq9ti9oUIF0sJ8it1RTNr3PHi_gURF0qglVLyOcNVSrAsRRu2C3WQApcfD0eh9NLdzf9My0b9w' type='text/javascript'> </script>
    <script language='javascript' type='text/javascript'>
    function getabc(){
        var eventlocation;
        var volunteerlocation;
        var s;
        eventlocation = new GLatLng($lat, $lng);
        //alert(eventlocation);
        var volunteerDist = new Array();
        s = $ctr;
        var tvid = new Array();
        for(s=0;s<$numrows;s++){
            //alert(s);
            //alert($vlat[0]);
            //alert($vlat[1]);
            //alert($vlat[s]);
        }
    }
    a = getabc();     < /script>";
7
  • $ctr is declared before this and it is initially set to 0 (zero) Commented Mar 15, 2012 at 9:37
  • what is $vlat set to? it looks like that variable doesn't contain anything at index s Commented Mar 15, 2012 at 9:39
  • @jlb $ctr is initially set to zero, s is set to variable $ctr mainly because for some reasons, using $ctr in the for loop gives an error Commented Mar 15, 2012 at 9:40
  • I believe the issue is you're accessing the PHP array $vlat (on the server side) with an index value only later defined in the JS part (on the client side). Commented Mar 15, 2012 at 9:41
  • 1
    Try print_r($vlat) before your echo statement to see what that variable contains. Commented Mar 15, 2012 at 9:41

2 Answers 2

1
alert($vlat[0]);
alert($vlat[1]);
alert($vlat[s]);

$vlat[0], $vlat[1] and $vlat[s] are parsed on the server before they is sent to the client. The first two can be resolved, but PHP does not know what s is, since s is only defined once the client side is reached.

Edit from chat discussion

$json = json_encode($vlat);

echo "<script language='javascript' type='text/javascript'>
   function test(){
       var obj = JSON.parse('$json');
       alert(obj);
   }
   < /script>";
Sign up to request clarification or add additional context in comments.

5 Comments

You can serialise the PHP array to JSON, so it can be accessed by the JavaScript. See stackoverflow.com/a/4885796/247702
var obj = JSON.parse('<?php echo json_encode($vlat); ?>'); does not work.. there is an error
it just does not show anything.. meaning there is an error.. the process stops after this line is called
When you look at the source of the rendered page, what are you seeing in place of __THIS__ in var obj = JSON.parse('__THIS__');?
0

The variable s that you are using to index the PHP array vlat is a variable that you declared in JavaScript. You cannot use it to index a PHP array.

What your code tries to do is to have PHP access a JavaScript variable, which it cannot. You can have PHP echo JavaScript, yes, but it doesn't work both ways.

3 Comments

so what do you suggest i should do?
I'd suggest passing in $vlat as an object into javascript, and looping through it in pure JS rather than trying to access the PHP variable in your JS loop. Something like var myArray = <?php echo json_encode($vlat);?>; might work.
var obj = <?php echo json_encode($vlat);?>; alert(obj); there is an error so alert() does not run;

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.