0

This form has multiple choices through a checkbox. Eg. Pet You Own is a multiple choice and there are various options such as Cat, Dog, Mule etc.

Now by default, the querystring sent will look like:

?pet=dog&pet=cat&pet=mule 

given all 3 are checked.

I need a way to parse this so that the querystring looks like:

?pet=dog,cat,mule

Another requirement is that, there are other parameters/inputs in the form so it needs to work in conjunction with other standard form inputs.

1
  • 1
    Why don't you use an array ? Name your inputs "pet" to "pets[]". Commented Dec 15, 2011 at 8:41

4 Answers 4

4

The format you're currently seeing is the conventional format. If your form fields were named pet[] rather than pet, your server would be able to interpret the result as an array.

Having said that, to actually do what you're requesting, you could reset the name attribute of your checkboxes, so that they won't be posted, and instead post a hidden field that holds the value of your checkboxes as a comma separated string:

$('#my-form').submit(function() {

    var pets = [];
    $('input[name=pet]:checked').each(function() {
        pets.push($(this).val());
    });

    // stop checkboxes from being posted
    $('input[name=pet]').attr('name','');

    // have an input field be posted instead
    $('#my-hidden-field')
        .val(pets.join(','))
        .attr('name', 'pet');

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

Comments

1

A bit of cleaning is needed but using this with plain JS you can acheive

<html>
    <head>
    <title>My Page</title>
    <script>
    function myFunction(){
    var options = "";
    if(document.getElementById("option1").checked){
    options = options+"Milk";
    }
    if(document.getElementById("option2").checked){
    options = options+",Butter";
    }
    if(document.getElementById("option3").checked){
    options = options+",Cheese";
    window.location = "end.html&options="+options
    }
    }
    </script>
    </head>
    <body>

    <div align="center"><br>
    <input id="option1" type="checkbox" name="option1" value="Milk"> Milk<br>
    <input id="option2" type="checkbox" name="option2" value="Butter" checked> Butter<br>
    <input id="option3" type="checkbox" name="option3" value="Cheese"> Cheese<br>
    <br>

    </div>
    <a href="#" onClick="myFunction();">Button to submit </a>
    </body>
    </html>

Comments

1

I suggest you to do this job on server side. When your server receive this request, it will get an array which is called pet and has three element: dog,cat and mule. you can conjunction them easily.

==== I implement this with JavaScript:

var str = window.location.href;
var queryString = "", temp = {};
str = str.substring(str.lastIndexOf("?") + 1);
str.split("&").some(function(item) {
var tarr = item.split("=");
    if(typeof temp[tarr[0]] == "undefined") {
        temp[tarr[0]] = tarr[1];
    } else if(typeof temp[tarr[0]] == "string") {
        temp[tarr[0]] += "," + tarr[1];
    }
});
// Make queryString
for(var i in temp) {
    queryString += "&" + i + "=" + temp[i];
}
queryString = queryString.replace(/^./,"");

// 
var href = window.location.href;
console.log("before:", href);
href = href.replace(/\?.*$/, "?");
// the url is that you want
console.log("after:", href + queryString);
//window.location.href = href + queryString;

OUTPUT:

before: http://www.boutell.com/newfaq/creating/forcedownload.html?pet=dog&pet=cat&pet=mule&animal=camel
after: http://www.boutell.com/newfaq/creating/forcedownload.html?pet=dog,cat,mule&animal=camel

Comments

0

Name your check boxes as p1, p2 etc. Have a hidden field in your form named 'pet'. Just before submit using JS, set the value of your hidden variable the way you need and return true.

function beforeSubmit() {
  var p = '';
  if($('#p1').attr('checked')==true) p += ',cat';
  if($('#p2').attr('checked')==true) p += ',dog';
  ...
  p = p.substring(1); // strip the , at 0
  $('#pet').val(p);
  return true;
}

and your form should be like:

<form ... onsubmit="return beforeSubmit()">
...
<input type="checkbox" name="p1" id="p1">Cat<br>
<input type="checkbox" name="p2" id="p2">Dog<br>
...
<input type="hidden" name="pet" id="pet" value="">
</form>

Comments

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.