How to POST empty <select .. multiple> HTML elements if empty?

早过忘川 提交于 2019-12-03 22:55:07
Edward Dale

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

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

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

EDIT:

Add a hidden field whenever the multiple select is present 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();

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

This way I was able to distinguish 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 code:

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

You can add a - please select - entry and 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>
</select>

Add something like

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

what if even when the user selects it, nothing 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?>" />
                <?php
                    $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">
                    <?php
                        while($opc = mysql_fetch_object($resOpc)){
                    ?>
                    <option value="<?=$opc->descricao?>"><?=$opc->descricao?></option>
                    <?php
                        }
                    ?>
                </select>
                <?php
                    }
                ?>
                <button class="botao" type="submit">Adicionar ao Carrinho</button>
            </form>

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

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

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

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

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

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


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

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

(This question has nothing to do with jQuery)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!