简体   繁体   English

如何将输入字段添加到表单

[英]How to add input fields to a form

I have a form and need to add fields/rows to it. 我有一个表单,需要向其添加字段/行。 My code adds the input fields, but I can't get the values after submit the form. 我的代码添加了输入字段,但是提交表单后我无法获取值。

HTML HTML

<form action="#" method="POST">
<div class="col-md-12">
    <fieldset class="fieldset-language" data-count="1">
        <legend>Legg til kostnader</legend>
        <div class="fieldset-content">
            <div class="row">
                <div class="col-md-5 form-group ">
                    <label for="">Cost</label>
                    <select name="verdi[]" class="form-control" >                                   
                        <?php foreach($kostnader as $Cost) { ?>
                            <option value="<?php echo $Cost; ?>"><?php echo $Cost;?></option>
                        <?php } ?>                                  
                    </select>
                </div>
                <div class="col-md-5 form-group ">
                    <label for="">Verdi</label>                                                         
                    <input type="text" class="form-control" id="Totalt" name="kostnad[]" placeholder="Some value">
                </div>

                <div class="col-md-2 form-group ">

                </div>
            </div>

        </div>
        <div class="input_fields_wrap"></div>

        <button class="add_field_button btn btn-primary" data-fields="0" name="leggtil">+ Add</button><br><br>
        <button type="submit" name="submit" class="btn btn-danger btn-lg">Send</button>

    </fieldset>
</div>

If form is submited, dump the content. 如果提交表单,则转储内容。

$post = array();

 for($i=0;$i<count($_POST['kostnad']);$i++){

      $post = [
       'kostnad'      => $_POST['kostnad'][$i],
       'verdi'      => $_POST['verdi'][$i]
      ];

 }

The jQuery code, that adds and clones the input field: jQuery代码,用于添加和克隆输入字段:

$(document).ready(function() {

    var max_fields = 10; //maximum input boxes allowed

    $(".add_field_button").click(function(e){ //on add input button click
        e.preventDefault();

        var add_button = $(this);
        var fieldset   = add_button.closest('fieldset');
        var wrapper    = add_button.closest('.sf-viewport');
        var form       = add_button.closest('form');
        var x          = parseInt(fieldset.attr('data-count'));
        var cur_height = 0;
        var fieldset_clone;
        var fieldset_content;

        if(x < max_fields){ //max input box allowed

            //text box increment
            fieldset.attr('data-count',x+1);


            cur_height = fieldset.height();
            form.height(cur_height*2);
            wrapper.height(cur_height*2);

            fieldset_clone = add_button
                            .closest('fieldset')
                            .find('.fieldset-content')
                            .eq(0)
                            .clone();

            fieldset_clone
                .find('[name]')
                .each(function(){
                    $(this).val($(this).prop('defaultValue'));
                });


            fieldset_content = $('<div>')
                .addClass('fieldset-content')
                .append(fieldset_clone.children());


            // add_button.before(fieldset_clone.children());
            add_button.before(fieldset_content);

            // add remove button
            add_remove_btn(fieldset_content);

        }
    });


    function add_remove_btn(item){
        item.prepend(
            '<a class="btn pull-right om-remove btn-danger btn-xs" href="#">'+
            '  <div class="small">Remove</div>'+
            '</a>'
        );

    }

    //user click on remove text
    $(document).on("click",".om-remove", function(e){

        e.preventDefault();

        var x = parseInt($(this)
            .closest('fieldset')
            .attr('data-count')
        );

        $(this)
            .closest('fieldset')
            .attr('data-count',x-1);

        $(this)
            .closest('.fieldset-content')           
            .slideUp(function(){
                $(this).remove()
            })

    });
});

If button is submited, run this var dump: var_dump($_POST); 如果提交了按钮,请运行以下var dump:var_dump($ _ POST);

Problem is that I can't fetch the data on the field added, only on the first entry, is there a way to fetch the data of the added fields? 问题是我无法仅在第一个条目上获取添加的字段上的数据,有没有办法获取添加字段的数据?

If you look at navigator debugger when doing post, all variables are there, as you named it with brackets []. 如果在发布时查看导航调试器,则所有变量都在那里,因为您使用方括号[]对其进行了命名。 As said here Multiple inputs with same name through POST in php , your POST values are defined into an Array for each variable name. 如此处所说,通过php中的POST 输入多个具有相同名称的输入 ,您的POST值被定义为每个变量名称的数组。 So, your code is working, but you might prefer to have a multidimensional array to save data: 因此,您的代码正在运行,但是您可能更希望使用多维数组来保存数据:

$post = array();

for($i = 0; $i < count($_POST['kostnad']); $i++) {

     $post[$_POST['verdi'][$i]] = [
      'kostnad'    => $_POST['kostnad'][$i],
      'verdi'      => $_POST['verdi'][$i]
     ];
}

echo "<pre>";
print_r($post);
echo "</pre>";

In this example, every value is saved on a $post subarray, indexed by 'verdi'. 在此示例中,每个值都保存在$ post子数组中,并由“ verdi”索引。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM