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
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:
- How to pass javascript jQuery variable value in php array?Best solution by Stack Overflow
- How to change javascript values to php?Best solution by stackoverflow.com
- Why is Javascript called Javascript, if it has nothing to do with Java?Best solution by Stack Overflow
- How to get JavaScript value in PHP?Best solution by Stack Overflow
- How to call Javascript function in PHP?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.