0

I have a search form:-

<form method="get" action="/jobs/">
        <select name="job_sector">
            <option value="">All sectors</option>
            <option value="Finance">Finance</option>
            <option value="Human Resources">Human Resources</option>
            <option value="Procurement &amp; Supply Chain">Procurement &amp; Supply Chain</option>
            <option value="Sales &amp; Marketing">Sales &amp; Marketing</option>
        </select>
        <select name="job_salary_from">
            <option value="0">Salary range</option>
            <option value="10000">£10,000 - £19,999</option>
            <option value="20000">£20,000 - £29,999</option>
            <option value="30000">£30,000 - £49,999</option>
            <option value="50000">£50,000 - £69,999</option>
            <option value="70000">£70,000 - £99,999</option>
            <option value="100000">£100,000+</option>
        </select>
    </div>
    <div class="col-md-6">
        <select name="job_type">
            <option value="">All job types</option>
            <option value="Permanent">Permanent</option>
            <option value="Temporary">Temporary</option>
            <option value="Interim">Interim</option>
        </select>
        <select name="job_location">
            <option value="">All job locations</option>
            <option value="Leicestershire">Leicestershire</option>
            <option value="Nottinghamshire">Nottinghamshire</option>
            <option value="Derbyshire">Derbyshire</option>
            <option value="Lincolnshire">Lincolnshire</option>
        </select>
    <input id="search-jobs" name="job_search" type="text" placeholder="Search DISTINCT for jobs e.g. Accountant">'
</form>

If I perform a search without setting anything, the URL becomes:-

http://website.dev/jobs/?job_sector=&job_salary_from=0&job_type=&job_location=&job_search=

Is there any way of not showing these in the URL if the value is empty, so the search URL would become:

http://website.dev/jobs/

If no value is set, and:-

http://website.dev/jobs/?job_sector=finance

If only one value was selected.

EDIT

I've tried:-

jQuery("form").submit(function(){
    jQuery("input").each(function(index, obj){
        if(jQuery(obj).val() == "") {
            jQuery(obj).remove();
        }
    });
});

But it still doesn't seem to be working.

nsilva
  • 5,184
  • 16
  • 66
  • 108

1 Answers1

2

You can't do it with html only, you'll need javascript for that.

Take a look at Submit only non empty fields from Form

Community
  • 1
  • 1
Gwendal
  • 1,273
  • 7
  • 17
  • Made an edit, I've tried the jQuery code from that post but it's still displaying everything. @Gwendal – nsilva Feb 04 '16 at 14:32
  • @nsilva did you include jquery libary? – Mark Ng Feb 04 '16 at 15:02
  • @nsilva js script was for input elements, you are using select. Juste replace jQuery('input') with jQuery('select') – Gwendal Feb 04 '16 at 15:24
  • Thanks, do you think I should change is to POST instead of GET though? – nsilva Feb 04 '16 at 15:42
  • @nsilva Depends of your needs. If you want your user to be able to bookmark results, keep get parameters. If you don't mind about it, you can switch to post and remove the js script – Gwendal Feb 04 '16 at 15:47