Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a JavaScript variable from PHP

I need to access a JavaScript variable with PHP. Here's a stripped-down version of the code I'm currently trying, which isn't working:

<script type="text/javascript" charset="utf-8">     var test = "tester"; </script>  <?php     echo $_GET['test']; ?> 

I'm a completely new to both JavaScript and PHP, so I would really appreciate any advice.

UPDATE: OK, I guess I simplified that too much. What I'm trying to do is create a form that will update a Twitter status when submitted. I've got the form working OK, but I want to also add geolocation data. Since I'm using Javascript (specifically, the Google Geolocation API) to get the location, how do I access that information with PHP when I'm submitting the form?

like image 300
Chris Armstrong Avatar asked Feb 26 '10 01:02

Chris Armstrong


People also ask

How use JavaScript variable in PHP SQL query?

php $var1 = $_POST['var1']; $var2 = $_POST['var2']; $getvalue="SELECT id,name from table1 WHERE column1='$var1' and column2='$var2'"; $result=mysql_query($getvalue) or die(mysql_error()); while($row=mysql_fetch_array($result)){ extract($row); echo $name; } ?>

How use JavaScript variable on same page in PHP?

You can easily get the JavaScript variable value on the same page in PHP. Try the following codeL. <script> var res = "success"; </script> <? php echo "<script>document.

How do you access variables in JavaScript?

In JavaScript, variables can be accessed from another file using the <script> tags or the import or export statement. The script tag is mainly used when we want to access variable of a JavaScript file in an HTML file. This works well for client-side scripting as well as for server-side scripting.

How store JavaScript value in PHP?

After the execution of the javascript code and after assigning the relevant value to the relevant javascript variable, you can use form submission or ajax to send that javascript variable value to use by another php page (or a request to process and get the same php page).


1 Answers

The short answer is you can't.

I don't know any PHP syntax, but what I can tell you is that PHP is executed on the server and JavaScript is executed on the client (on the browser).

You're doing a $_GET, which is used to retrieve form values:

The built-in $_GET function is used to collect values in a form with method="get".

In other words, if on your page you had:

<form method="get" action="blah.php">     <input name="test"></input> </form> 

Your $_GET call would retrieve the value in that input field.

So how to retrieve a value from JavaScript?

Well, you could stick the javascript value in a hidden form field...

<script type="text/javascript" charset="utf-8">     var test = "tester";     // find the 'test' input element and set its value to the above variable     document.getElementByID("test").value = test; </script>  ... elsewhere on your page ...  <form method="get" action="blah.php">     <input id="test" name="test" visibility="hidden"></input>     <input type="submit" value="Click me!"></input> </form> 

Then, when the user clicks your submit button, he/she will be issuing a "GET" request to blah.php, sending along the value in 'test'.

like image 199
Pandincus Avatar answered Sep 22 '22 15:09

Pandincus