Reputation: 31
I am trying to pass value via url for two checkboxes, both with different names and only need to see if they are checked or unchecked. Here is what the html code looks like:
<input type="checkbox" name="one" value="one" checked id="one" >
<input type="checkbox" name="two" value="two" id="two" >
how do I encode this in url, so that the check boxes can be checked or unchecked by what's in the url, for example:
www.site.com?one=unchecked&two=checked
Upvotes: 3
Views: 9228
Reputation: 477
You could do it with a little php:
<input type="checkbox" name="one" value="one" id="one" <?= ($_GET["one"] == "checked" : "checked" : "" ?> >
<input type="checkbox" name="two" value="two" id="two" <?= ($_GET["two"] == "checked" : "checked='checked'" : "" ?>>
.... or as you mentioned it, on the client side with Javascript:
document.body.onload = function() {
var hash = window.location.hash().substr(1);
var parts = hash.split("&");
for(var i = 0; i < parts.length; i++) {
var variable = parts.split("=");
if(variable[0] == "one") // if the key of a get-variable equals "one"
document.getElementById("one").setAttribute("checked");
else if(variable[0] == "two") // if the key of a get-variable equals "two"
document.getElementById("two").setAttribute("checked");
}
};
I hope it helps...
Upvotes: 1