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

后端 未结 8 1854
醉酒成梦
醉酒成梦 2021-02-18 18:09

I have the following multi-select box in a HTML form, where user can select one or more option.


                        
    
提交评论

  • 2021-02-18 18:56

    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();
    
    0 讨论(0)
  • 2021-02-18 18:56

    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;

    0 讨论(0)
  • 2021-02-18 18:59

    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>
    
    0 讨论(0)
  • 2021-02-18 19:02

    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']="";
    
    0 讨论(0)
  • 2021-02-18 19:05

    Add something like

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

    0 讨论(0)
  • 提交回复
    热议问题