0

I have a form that contain a multiple checkbox i want to insert the values of checkbox into the url like this

var a=encodeURIComponent(document.getElementById("idofcheckbox").value)
xmlhttp.open("GET","InsertPHPData.php?q="+a+,true); xmlhttp.send();
Choix1
choix2
2
  • Easy if you agree to use JQuery. Do you? Commented Nov 3, 2012 at 17:13
  • why are you posting your html into a comments block?.. update your question so all pertinent info is in one place Commented Nov 3, 2012 at 17:24

1 Answer 1

2

Try this:

var x = document.getElementsByTagName("input");

var params = [];
for (var i = 0; i < x.length; i++) {
    if (x[i].type === "checkbox") //&& x[i].checked === true 
        params.push(x[i].name + "+" + x[i].value);
}

var url = "InsertPHPData.php";
url += "?" + encodeURI(params.join(","));

alert(url)​

Working fiddle. Please modify accordingly :)

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

4 Comments

Also dont seperate the params with a , but with a & and put a = between the name and value instead of a + to produce a valid url
Right... Thanks, i dont know what i was thinking
i have another input in my form so i can't make that have u another solution to distingue my input type=checkbox to another input
@Sirina - Added the type checking. Modified the answer.

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.