4

I have a form with a get method that is used for searching items (vehicles) from the database. The form has multiple checkbox sets. Like user can filter the search results by selecting multiple makes from the makes checkbox list. Similarly user can also apply filter on car models from models chebox list and so on. There are about 10 such filters on the form.

Here is the code snippet of the my form:

<form action='./search' method='get'>

<input type='checkbox' name='make[]' value='BMW'/> 
<input type='checkbox' name='make[]' value='Mercedes'/> 
<input type='checkbox' name='make[]' value='Honda'/> 
<input type='checkbox' name='make[]' value='Toyota'/> 
<input type='checkbox' name='make[]' value='Porsche'/> 

......//Remaining Form
</form>

Similarly for car models I have similar markup...There are such 10 filters all implemented using checkbox list.

Now coming towards the problem, when I submit the form I get URL like this:

http://localhost/auto/search?make=BMW&make=Mercedes&make=Honda

This is forming a type of query string which I don't like i.e it is repeating 'make' attribute for all the checked values and will do so for remaining 9 filters as well. This will result in very long ugly looking URL.

What I want is that my URL should look something like this:

http://localhost/auto/search?make=BMW,Mercedes,Honda

This is much better but I don't know how would I achieve that. What I have tried is to get all the values of checked boxes and then write them into hidden field value so that I get my desired format in the query string. And unset all the checboxes selected by the user and submit the form with hidden field containing all the selections. But the problem is when the form is submitted I get two 'make' fields in the URL like:

http://localhost/auto/search?make=&car_makes=BMW,Mercedes,Honda

where car_makes is the hidden input field in which I wrote all the selections in value attribute.

Any solution to this? So that make attribute does not get submitted, but it does, since it is the part of the form.

Thanks.

3
  • If you make the form a post instead, you won't have a mess on your URL and PHP will treat any submitted makes as an array. Commented Oct 30, 2014 at 15:12
  • 1
    @DigitalChris Using POST for a search is not that user friendly if you want to go back and forth, bookmark and share searches, etc. Commented Oct 30, 2014 at 15:16
  • @Chris...Users might want to share URLs as well with others, in that case post won't work. Thanks for your suggestion btw. Commented Oct 30, 2014 at 15:23

2 Answers 2

3

Try these:

<html>
<head>
    <script type="text/javascript">
        function buildup(){
            var makes=document.getElementsByName('make[]');
            var m=document.getElementById('make');
            m.value='';
            ms='';
            for (var i = makes.length - 1; i >= 0; i--) {
                if(i>0)ms=ms+',';
                ms=ms+makes[i].value;
            }
            m.value=ms;
            document.getElementById('form').submit();
        }
    </script>
</head>
<body>

    <input type='checkbox' name='make[]' value='BMW'/> 
    <input type='checkbox' name='make[]' value='Mercedes'/> 
    <input type='checkbox' name='make[]' value='Honda'/> 
    <input type='checkbox' name='make[]' value='Toyota'/> 
    <input type='checkbox' name='make[]' value='Porsche'/> 

    <form action='./search' id='form' method='get'>

        <input type='hidden' name='make' id='make'>

        <input type='button' value='submit' onclick='buildup()'>

    </form>

</body>

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

1 Comment

Seems like I would use your's and jeroen answer in combo. Thanks both of you.
1

If you don't want the make inputs to get submitted, you should disable them before the form gets submitted (or remove the name attribute...).

It would probably be easiest to add a class to all inputs you don't want to submit and then do something like this right before the form submit:

html:

...
<input type='checkbox' name='make[]' value='BMW' class='dont-send-class' /> 
...

js:

$('.dont-send-class').prop("disabled", true);
// now you can submit the form

1 Comment

That could be the way to go. Thanks!

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.