Question

I have this form with get set as method.

<form method="get" action="index.html">
    <input type="checkbox" name="item" value="1" />
    <input type="checkbox" name="item" value="2" />
    <input type="submit" />
</form>

When I submit, i end up with the url index.html?item=1&item=2

I wonder if it is possible to make a more neat format like comma-seperation, for instance index.html?item=1,2

I have been searching for a while, but I cannot find how to obtain this. Do I really need to implement this with javascript?

Was it helpful?

Solution

    

    function submitForm(){
    var name=document.getElementsByName('item[]');
    var str="";
    for(i=0;i<(name.length);i++){
      if(name[i].checked){
         str+=name[i].value+",";
      }
    }
    if(str.length>0){str=str.substring(0,str.length-1)};// remove the last comma
    var url="actionpage.html?item="+str;
    document.getElementById('formid').action=url;
    document.getElementById('formid').submit();
    }

Please rename the checkbox to item[], this will create an array. Then call this function in your button click event.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top