3

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
3
  • Do you have a server side scripting language or do you want to achieve it client side? Commented Jan 1, 2013 at 22:50
  • If you set your html form mode to GET, the form variables will be passed on the Url. It produce exactly what you're showing, but if you have control over the server-side of things, you have the information (implicitly). If this isn't on track, could you please clarify the parameters of the question? Commented Jan 1, 2013 at 22:54
  • 1
    I need to do this on the client side in the browser, not using the server side script. Cj, what I am trying to do is to be able to set checkbox to either checked or unchecked using the value from the url. For example, if the value for one is unchecked, the box will be unchecked. Commented Jan 1, 2013 at 23:07

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks atreju. If I have the above javascript on my page, how would I transfer that to the next page via url? What I am trying to do is when the link is clicked, I want that value to be passed to the next page <a href='www.site.com?box-one=unchecked&box-two=checked'>link</a> Is that possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.