HiDd3N
HiDd3N

Reputation: 494

how to not post empty select options from html to php

i have some select options in my form which will post data to my script.but

    <select name="city" id="City">
    <option></option>
    </select>
    <select name="country" id="Country">
    <option></option>
    </select>

but when they are empty they post data with no value somthing like this:

city=&country=&

i know i can unset them by unset command.but when i pass these data to javascript library(jquery) and serialize theme everyone can see them in firebug.how can i prevent empty select options to not post data?

thanks

Upvotes: 0

Views: 3680

Answers (3)

Samuel Cook
Samuel Cook

Reputation: 16828

If your using jQuery here is a quick mock up of something that you could adapt and utilize, where adding a disabled attribute will not submit that form element:

<form method="get" action="./">
    <select name="city" id="City">
        <option></option>
    </select>
    <select name="country" id="Country">
        <option></option>
    </select>
    <input type="submit">
</form>
<script type="text/javascript">
$(function(){
    $('form').submit(function(){
        $('select').each(function(){
            if($(this).val()==''){
                $(this).attr('disabled','disabled');
            }
        })

    })
})
</script>

Upvotes: 4

anuj arora
anuj arora

Reputation: 831

ok fine, if have empty selectbox. Then just put atleast one tag, like

<option value="0">No data available</option>

So you may get 0.

Upvotes: 1

Benjamin Paap
Benjamin Paap

Reputation: 2759

You can disable them by setting the disabled attribute like this:

<select name="city" disabled="disabled">
    ...
</select>

This should prevent the browser from sending this field in it's POST request.

Upvotes: 0

Related Questions