[ACCEPTED]-How to POST empty <select .. multiple> HTML elements if empty?-html-select

Accepted answer
Score: 23

If your form is generated dynamically, you 6 could include a hidden form element with 5 the same name that contains a dummy value. Then, just 4 ignore the dummy value, if the value you 3 get for that variable is ['dummy_value'] then you can treat 2 that as meaning "nothing selected" in your 1 code.

Score: 16

If you add a hidden input before the multiple 7 select element, it will send the hidden 6 input value if none of the multiple select 5 items have been selected. As soon as you 4 select an option though, that selected value 3 is used instead.

This way I was able to distinguish 2 2 different scenarios in Laravel/php, being:

  • Set all myitems to empty (requiring the hidden input, so PHP receives an empty string for myitems)
  • Update other properties of the model without touching myitems (so excluding any myitems input form the form. PHP will not receive the myitems key)

Sample 1 code:

<input type="hidden" name="myitems" value="" />
<select name="myitems[]" multiple>
  <option value="1">Foo</option>
  <option value="2">Bar</option>
Score: 12

Is there a reason you can't treat the situation 4 where the array isn't set as if it was sent 3 with no contents?

if (!isset($_POST['eng_0']))
    $_POST['eng_0'] = array();


Add a hidden field 2 whenever the multiple select is present 1 in your form:

<input type="hidden" name="eng_0_exists" value="1"/>

Then check:

if (!isset($_POST['eng_0']) && isset($_POST['eng_0_exists']))
    $_POST['eng_0'] = array();
Score: 2

You can add a - please select - entry and 1 preselect it.

<select id="eng_0" name="eng_0[]" multiple size="3">
  <option value="nothing" selected="selected">- please select -</option>
  <option value="Privilégier">Privilégier</option>
  <option value="Accepté">Accepté</option>
  <option value="Temporaire">Temporaire</option>
Score: 2

Add something like

    $("#field").change(function() {
      if (($("#field").val() || []) == "") $("form").append("<input type='hidden' name='field' value=''>");


Score: 1

what if even when the user selects it, nothing 5 about the select arrives in the $_POST?

            <form action="cart.php" method="POST">
                <input type="hidden" id="acao" name="acao" value="add" />
                <input type="hidden" id="id" name="id" value="<?=$cnt->cnt_codigo?>" />
                    $resOpc = mysql_query("SELECT * FROM loja_opcionais ORDER BY descricao")or die(mysql_error());
                    if(mysql_num_rows($resOpc) > 0){
                <select id="opcional" nome="opcional">
                        while($opc = mysql_fetch_object($resOpc)){
                    <option value="<?=$opc->descricao?>"><?=$opc->descricao?></option>
                <button class="botao" type="submit">Adicionar ao Carrinho</button>

When 4 I do print_r($_POST); on the other side 3 the output is simply: Array ( [acao] => add 2 [id] => 3 ) and no sign of the select tag, even 1 when I change the value on the other side;

Score: 0

if there is no $_POST, Post an empty string 5 (nothing) is absolutely the most simple 4 solution.

Here my solution for a Form with 3 a <select name="related[]" multiple>

just add the following line in the php 2 section that handles the storing of your 1 form:

if (!isset($_POST['related'])) $_POST['related']="";
Score: 0

Include <input type="hidden" name="yourfield" value="" /> to your form where the name is 7 the same as for your multiple="multiple" select.

It is unfortunate 6 that browsers do not send empty form parameters 5 for multiple="multiple" selects like they do for non-multiple 4 selects or a normal inputs.

Using a dummy 3 value as proposed in the accepted answer 2 is not a very clean solution.

(This question 1 has nothing to do with jQuery)

More Related questions