Reputation: 2075
I am trying to create a script that will get all keys from the url (url/?green&red&blue). I know I can use the $_GET array for that, but I don't fully understand how to use it.
What I want is a for loop that gets all values in order and then does something with it, for example print it.
I'm stuck here right now so if anyone can help me further it will be much appreciated!
Upvotes: 1
Views: 1235
Reputation: 97
If you want to get them in javascript, here is the code snippet.
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
The function returns an array/object with your URL parameters and their values.
Upvotes: -1
Reputation: 813
Just to point out. if your url is (url/?green&red&blue)
You will get an array like this.
Array
(
[green] =>
[red] =>
[blue] =>
)
Your url will need to look like this ( url/?colora=green&colorb=red&colorc=blue ) So you get:
Array
(
[colora] => green
[colorb] => red
[colorc] => blue
)
Upvotes: 0
Reputation: 11240
You could use something like:
foreach($_GET as $key => $value){
echo "$key: $value<br />";
}
remember that you should sanitize your user input, so do not use the code in a production environment.
Upvotes: 7
Reputation: 8079
foreach ($_GET as $key => $value){
echo $key.' : '.$value.'<br />';
}
Upvotes: 4