简体   繁体   English

克隆具有数组名称的输入字段仅获得 php 中的第一个值?

[英]Cloning input fields with array name only get the first value in php?

I clone my input fields using jquery.我使用 jquery 克隆我的输入字段。 and get the value in PHP using var_dump first并首先使用 var_dump 获取 PHP 中的值

Example例子

<form action="save.php" method="POST">
<div id="div-container">
 <div class="div-clone">
   <input name="name[]">
   <input name="bday[]">
   <input name="address[]">
   <input name="gender[]">
 </div>
</div>
<input type="submit" value="SUBMIT">
</form>

Now when then I'm cloning it using现在什么时候我正在使用它克隆它

$('#div-container').append($('#div-clone').get(0).outerHTML);

PHP CODE PHP 代码

if(isset($_POST['SUBMIT'])
{
 var_dump($_POST);
}

Does this result the array key value is also the same as the first one?这是否导致数组键值也与第一个相同? because I only get one value and only the first one.因为我只得到一个值,而且只有第一个值。 Is there any way to solve this?有没有办法解决这个问题?

Thanks谢谢

EDIT I already getting data from my form my only problem is the input array that I clone.编辑我已经从我的表单中获取数据我唯一的问题是我克隆的输入数组。 it's only getting the first value.它只获得第一个值。

EDIT AGAIN When I try to add just input with the same name of an array without cloning its works perfectly.再次编辑当我尝试添加具有相同名称的数组输入而不完美克隆其作品时。 Any solution regarding this?有什么解决方案吗? Thanks谢谢

Firstly remove the id from the content you want to clone.首先从要克隆的内容中删除id If you don't you'll end up with duplicates which is invalid and can cause issues in JS and the UI.如果不这样做,您最终会得到无效的重复项,并可能导致 JS 和 UI 出现问题。 A better approach is to use classes.更好的方法是使用类。

In addition, if you want to copy elements simply use clone() .此外,如果您想复制元素,只需使用clone() Try this:尝试这个:

 $('button').on('click', () => $('.div-clone:first').clone().appendTo('#div-container'));
 input { width: 100px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button>Clone</button> <div class="div-clone"> <input name="name[]"> <input name="bday[]"> <input name="address[]"> <input name="gender[]"> </div> <div id="div-container"></div>

Output from your code is您的代码中的 Output 是

array(4) {
  ["name"]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
  }
  ["bday"]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
  }
  ["address"]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
  }
  ["gender"]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
  }
}

So it's correct.所以是正确的。 I cloned once, so I get two arrays of "name", two arrays of "bday" etc. I guess that you want another format, this is a little trickier I think.我克隆了一次,所以我得到了两个“名称”的 arrays,两个“bday”的 arrays 等。我想你想要另一种格式,我认为这有点棘手。 Try this:尝试这个:

<button id="clone">Clone</button>

    <form method="POST">
        <div id="div-container">
            <div class="div-clone">
                <input data-field="name" name="data[0][name]">
                <input data-field="bday" name="data[0][bday]">
                <input data-field="address" name="data[0][address]">
                <input data-field="gender" name="data[0][gender]">
            </div>
        </div>
        <button type="submit">Submit</button>
    </form>

    <pre>
    <?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        var_dump($_POST);
    }
    ?>
    </pre>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        var i = 0;
        $('button#clone').on('click', () => {
            const clone = $('.div-clone:first').clone();
            i++;
            clone.find('input').each(function() {
                const fieldname = $(this).attr('data-field');
                $(this).attr('name', 'data[' + i + '][' + fieldname + ']');
            });
            clone.appendTo($('#div-container'));
        });
    </script>

This will give you the following output for one clone.这将为您提供一个克隆的以下 output。

array(1) {
  ["data"]=>
  array(2) {
    [0]=>
    array(4) {
      ["name"]=>
      string(1) "1"
      ["bday"]=>
      string(1) "1"
      ["address"]=>
      string(1) "1"
      ["gender"]=>
      string(1) "1"
    }
    [1]=>
    array(4) {
      ["name"]=>
      string(1) "2"
      ["bday"]=>
      string(1) "2"
      ["address"]=>
      string(1) "2"
      ["gender"]=>
      string(1) "2"
    }
  }
}

The field name is exactly the PHP-Array structure.字段名称正是 PHP-Array 结构。 Using name[] is just an array counting up.使用 name[] 只是一个向上计数的数组。 But I think all four fields should make one entry.但我认为所有四个字段都应该输入一个条目。 So you have to make one entry (named data), make it an array, which is counted up (by "i") and then you add the field name (name, bday etc.)因此,您必须创建一个条目(命名数据),使其成为一个数组,该数组被计数(按“i”),然后添加字段名称(名称、生日等)

After messing around for so many times I solved it.折腾了好多次,终于解决了。

It seems on my framework that there's a UI error that make my form end on the near div element after cloning.在我的框架上似乎有一个 UI 错误,使我的表单在克隆后在 near div 元素上结束。 Thank you for all your answer.谢谢你所有的回答。 all your answer is right so I'll upvote them.你所有的答案都是正确的,所以我会支持他们。 Thank you.谢谢你。

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

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