How Can I add Latitude, Longitude dynamically in Javascript?

Passing values between php and javascript?

  • i want to send a variable from php to javascript. something like this php file, <?php $userid = "Snib"; ?> <script language="javascript" type="text/javascript"> var userid = "<?php echo $userid; ?>"; alert('Welcome to this page, ' + userid); </script> i am not sure whether if this is valid since php is executed in the server and javascript is executed in a browser. i want this because , i want to fetch latitude and longitude coordinates from mysql using php and display it in a webpage using googlemaps javascript api. thanks in advance !

  • Answer:

    this syntax is fine... I find no problem in this. Go on and have a test on server.

Achuthan at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

this syntax is fine... I find no problem in this. Go on and have a test on server.

Dream Window

It won't work. For precisely the reason you specify. PHP variables only exist on the server. Javascript runs in the browser. What you need to do is to use PHP to generate the JavaScript code. Taking your code as an example, it might be something like: <?php $userid = "Snib"; echo "<script language=\"javascript\" type=\"text/javascript\">"; echo "var userid = " . $userid . ">"; echo "alert('Welcome to this page, ' + userid);"; echo "</script>"; ?> You get the idea? That way, you end up with JavaScript in the browser that contains the value of the PHP variable.

Schifreen

It won't work. For precisely the reason you specify. PHP variables only exist on the server. Javascript runs in the browser. What you need to do is to use PHP to generate the JavaScript code. Taking your code as an example, it might be something like: <?php $userid = "Snib"; echo "<script language=\"javascript\" type=\"text/javascript\">"; echo "var userid = " . $userid . ">"; echo "alert('Welcome to this page, ' + userid);"; echo "</script>"; ?> You get the idea? That way, you end up with JavaScript in the browser that contains the value of the PHP variable.

Schifreen

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.