views:

494

answers:

5

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 complete noob at both js and php, so 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, I need to somehow access that info with php when I'm submitting the form.

+1  A: 

_GET accesses query string variables, test is not a querystring variable (PHP does not process the JS in any way). You need to rethink. You could make a php variable $test, and do something like:

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

<?php
    echo $test;
?>

Of course, I don't know why you want this, so I'm not sure the best solution.

EDIT: As others have noted, if the JavaScript variable is really generated on the client, you will need AJAX or a form to send it to the server.

Matthew Flaschen
How do I make it a querystring variable? Or do I need to use a different php method?
Chris Armstrong
You could submit a HTML form with method="GET".
Matthew Flaschen
A: 

JS ist browser-based, PHP is server-based. You have to generate some browser-based request/signal to get the data from the JS into the PHP. Take a look into Ajax.

Leonidas
+4  A: 

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'.

Pandincus
Thanks, I've updated my question to clarify what I'm trying to achieve. Do you think the above solution (invisible form field) is still the best solution?
Chris Armstrong
You could also use XHR to load your page and put the get variables in yourself. This would remove the form submitting... basiclaly, you would load the url "asdf.php?test="+test.value
ItzWarty
@Chris Armstrong Sure, it's a fine solution. However, I don't know how much data that is, and I _believe_ that GET requests have a limit of about 2kb of data (not researched, just an assumption). Youmay want to consider using a POST instead of a GET to send your form data. Additionally, this way the data won't appear in the URL field of the browser.Something else to consider - you may set the geolocation data when the page loads, but what if the user is in a different place when they click 'submit'? In this case, you may want to use javascript to submit the form: http://bit.ly/qOEhx
Pandincus
@pandincus The invisible form field does seem to be working fine, and the geolocation data is only two coordinates so size shouldn't be a problem, however I am looking at using ajax (specifically, jquery) to submit the form, so do you think I'd be better waiting till the user hits submit before looking for their location?
Chris Armstrong
@Chris Armstrong jquery is a great choice, and absolutely I think it would be better to use a regular button instead of the form submit button, use jquery to bind a function to the button's "click" event, and at that point capture the geolocation and then use javascript to submit the form.
Pandincus
+3  A: 

As JavaScript is a client-side language and PHP is a server-side language you would need to physically push the variable to the PHP script, by either including the variable on the page load of the PHP script (script.php?var=test), which really has nothing to do with JavaScript, or by passing the variable to the PHP via an AJAX/AHAH call each time the variable is changed.

If you did want to go down the second path, you'd be looking at XMLHttpRequest, or my preference, jQuerys Ajax calls: http://docs.jquery.com/Ajax

Decipher
A: 

If showing data to the user, do a redirect:

<script language="JavaScript">
    var tester = "foobar";
    document.location="http://www.host.org/myphp.php?test=" + tester;
</script>

or an iframe:

<script language="JavaScript">
    var tester = "foobar";
    document.write("<iframe src=\"http://www.host.org/myphp.php?test=" + tester + "\"></iframe>");
</script>

If you don't need user output, create an iframe with width=0 and height=0.

Hawkcannon