How to append multiple values to a single parameter in html form?

后端 未结 4 1153
迷失自我
迷失自我 2021-01-12 03:48

Assuming I have the following form:

Oats
相关标签:
4条回答
  • 2021-01-12 04:08

    If you want several values in one parameter, you have to name it like parameter[]

    f.e.:

    <form action="process.php">
    <input type="checkbox" name="parameter[]" value="oats"> Oats
    <input type="checkbox" name="parameter[]" value="beans"> Beans
    <input type="hidden" name="parameter[]" value="a"/>
    <input type="submit" value="Submit"/>
    </form>
    
    0 讨论(0)
  • 2021-01-12 04:11

    I was really only interested in checkboxes that were actually checked - so I built myfunc() from the other examples like this:

    function myfunc(){
        $('#void input[type="checkbox"]').each(function(id,elem){
            if ( ! $(this).is(':checked') ) return;
            b = $("#res").val();
            if(b.length > 0){
                $("#res").val( b + ',' + $(this).val() );
            } else {
                $("#res").val( $(this).val() );
            }
        });
        var stuff = $("#res").val();
        if ( stuff.length < 1 ) {
            alert('Please select at least one');
        } else {
            $("#real").submit();
        }
    }
    
    0 讨论(0)
  • 2021-01-12 04:20

    More or less here the idea: first form, 1 hidden input will catch all the vaules of second and void form

    <script type="text/javascript">
    function myfunc(){
        $('#void input').each(function(id,elem){
             b = $("#res").val();
             if(b.length > 0){
                $("#res").val( b + ',' + $(this).val() );
            } else {
                $("#res").val( $(this).val() );
            }
    
        });
            alert("VAL "+$("#res").val());
        $("#real").submit();
    
    return false;
    }
    
    </script>
    
    <form id="real" action="process.php">
    <input id="res" type="hidden" name="parameter" value=""/>
    </form>
    
    <form id="void">
    <input type="checkbox" name="option1" value="oats"> Oats
    <input type="checkbox" name="option2" value="beans"> Beans
    <input type="hidden" name="parameter" value="a"/>
    <input type="submit" value="Submit" onClick="javascript:myfunc()"/>
    </form>
    

    add some fix, submit the right form. Tested on jsfiddle:

    JsFiddel "working" example

    ADD SINGLE FORM VERSION

    You've to know the name of the field, and ignore the other params in the answer

    <script type="text/javascript">
    function myfunc(){
        // can use more selector
        $('#real input').each(function(id,elem){
            // append the data to this field so no need to process it
            if(this.id == 'res'){
                return;                
            }
            // here I concat the values 
            b = $("#res").val();
            if(b.length > 0){
                $("#res").val( b + ',' + $(this).val() );
            } else {
                $("#res").val( $(this).val() );
            }
    
        });
        alert("VAL "+$("#res").val());  // remove me
        $("#real").submit();
    
        return false;
    }
    
    </script>
    
    <form id='real' method='POST' action="#">
    <input type="checkbox" name="option1" value="oats"> Oats
    <input type="checkbox" name="option2" value="beans"> Beans
    <input id="res" type="hidden" name="parameter" value="a"/>
    <input type="submit" value="Submit" onClick="javascript:myfunc()"/>
    </form>​
    
    0 讨论(0)
  • 2021-01-12 04:24

    There is a very useful method to use for dynamic data grouping.

    <form action="file.php" method="post">
    
    <?php for ( $i = 0; $i < count( $something ); $++ ) { ?>
        <input type="checkbox" name="<?php $something[$i]; ?>[]" value="user_id"> Oats
        <input type="checkbox" name="<?php $something[$i]; ?>[]" value="user_name"> Beans
    <?php } ?>
    <input type="submit" value="Submit"/>
    </form>
    
    0 讨论(0)
提交回复
热议问题