Sirina
Sirina

Reputation: 21

how to insert multiple checkbox value in the url

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

Upvotes: 0

Views: 1292

Answers (1)

bhb
bhb

Reputation: 2561

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 :)

Upvotes: 2

Related Questions