处理动态表单表单、动态

2023-09-11 01:06:44 作者:酒妓

我有一个JS动态表单,在那里我可以添加或删除字段:

I've a JS dynamic form, where i can add or remove fields:

Field 1
Field 2
Field 3
Field 4

现在,如果我删除字段2,并添加一个新的领域:

Now if I remove Field 2 and add a new field:

Field 1
Field 3
Field 4
Field 5

我的表单元素中发送此通过Ajax的POST。 我的问题是服务器端PHP处理。我怎么能知道我有多少个字段有$ _ POST数组中和他们有什么证书?我生成唯一的ID使用田+ counterIndex,但下面的例子中,我怎么能看得懂,我一共有4场和2号缺失有哪些?顺便说一句,在窗体中我有太多的静态字段。

I'm sending this through Ajax POST inside a form element. My problem is server side PHP processing. How can I know how many fields I have inside $_POST array and what are their ids? I'm generating unique id using "field" + counterIndex, but following the example how can I be able to understand that I have a total of 4 fields and that number 2 is missing? By the way, inside the form I have static fields too.

推荐答案

这可以通过PHP做的 $ _ POST 本身是一个数组因此可以循环。

This can be done through PHP as $_POST is itself an array thus it can be looped.

我说,你有字段:

<输入名称=DYN [ID1]/>

<输入名称=DYN [ID2]/>

在后端PHP文件,

循环遍历 $ _ POST 如下:

<?php
if (isset($_POST['SUBMIT_BTN'])) {
  if (! empty($_POST['dyn'])) {
    foreach ($_POST['dyn'] as $dyn_id => $dyn_val) {
     // "$dyn_id" is your ID you needed.
    }
  }
}
?>