0

Possible Duplicate:
Get query string values in JavaScript

I know you could do this by echoing the variable $_GET['whatevervariable'] to a js variable.i was just wondering if there are other methods that can also do this?

Community
  • 1
  • 1
  • 1
    Can you be more specific? What do you want to do? – elclanrs Feb 27 '12 at 06:38
  • 1
    See http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript -- you can do it in pure javascript. – srubin Feb 27 '12 at 06:40
  • No, it’s not true that you can (normally) “echo” a variable from a PHP script into a JavaScript variable. Perhaps a code fragment will help others answer your questions. – danorton Feb 27 '12 at 06:40
  • aw my topics going to be shut down again for being unclear.anyway i just want to know if there are other ways of getting the $_GET['variable'] from the url and passing it to a js variable without using – Kester Soriano Feb 27 '12 at 06:43

2 Answers2

1

This will provide the entire $_GET array to your JavaScript app:

<script type="text/javascript"><!--
_GET = <?php echo json_encode($_GET); ?>;
--></script>
danorton
  • 11,804
  • 7
  • 44
  • 52
0

The $_GET superglobal is just an array of the querystring.

You can fetch the querystring in javascript with window.location.search, but to use it like $_GET will need some sort of parsing and usually a few regular expressions to handle difficult characters etc.

Just try var qs = window.location.search; and then figure out exactly what you need, and how you will get it.

The easy solution if doing it inline is just echoing $_GET into a variable, like in danorton's answer.

adeneo
  • 312,895
  • 29
  • 395
  • 388