简体   繁体   English

提取动态创建的表单数据

[英]Extract dynamically created form data

I've just started using jQuery. 我刚刚开始使用jQuery。 One thing I've been using it for is adding rows to a table that is part of a form. 我一直在使用它的一件事是向表的一部分添加行。

When I add a new row, I give all the form elements names like 'name_' + rowNumber . 当我添加新行时,我会给所有表单元素名称,例如'name_' + rowNumber name 'name_' + rowNumber I increment rowNumber each time I add a row. 每当我添加一行时,我都会增加rowNumber

I also usually have a Remove Row Button. 我通常也有一个“删除行”按钮。 Even when a row is removed, the rowNumber count stays the same to keep from repeating element names. 即使删除了一行, rowNumber计数也保持不变,以免重复元素名称。

When the form is submitted, I set a hidden element to equal the rowNumber value from jQuery. 提交表单后,我将一个隐藏元素设置为等于jQuery中的rowNumber值。 Then in PHP, I count from 1 to the rowNumber value. 然后在PHP中,我从1到rowNumber值进行计数。 Then for each value, I perform an isset($_REQUEST['name'_ . index]) . 然后,对于每个值,我执行一个isset($_REQUEST['name'_ . index]) This is how I extract the form elements that remained after deleting rows in jQuery. 这就是我提取在jQuery中删除行后剩余的表单元素的方式。

Does anyone here have a better technique for accounting for deleted rows? 这里有没有人有更好的技术来处理已删除的行?

For some of our simpler tables, we use a field name such as 'name[]', though for JavaScript they would need a usable id. 对于某些较简单的表,我们使用字段名,例如'name []',尽管对于JavaScript,它们将需要可用的ID。

It does add some complexity in that 'name[0]' has to assume 'detail[0]' is the correct element. 它确实增加了一些复杂性,因为“ name [0]”必须假定“ detail [0]”是正确的元素。

PHP will create an array and append elements if the field name ends with [] similar to 如果字段名称以[]结尾,PHP将创建一个数组并追加元素

<input name="field[]" value="first value" />
<input name="field[]" value="second value" />

// is roughly the same as

$_POST['field'][] = 'first value';
$_POST['field'][] = 'second value';

Use arrays to hold you values in your submission. 使用数组将值保留在提交中。 So bin the row count at the client side, and name your new elements like name[] . 因此,在客户端对行计数进行装箱,然后将新元素name[] This means that $_POST['name'] will be an array. 这意味着$_POST['name']将是一个数组。

That way at the server side you can easily get the row count (if you need it) with: 这样,在服务器端,您可以通过以下方式轻松获取行数(如果需要):

$rowcount = count($_POST['name']);

...and you can loop through the rows at the server side like this: ...并且您可以像这样在服务器端循环浏览各行:

for ($i = 0; isset($_POST['name'][$i]; $i++) {}

您可以通过执行foreach($ _ POST as $ key => $ value)提取所有行。

When adding a dynamic form element use the array naming method. 添加动态表单元素时,请使用数组命名方法。 for example 例如

<input type="text" name="textfield[]" />

When the form is posted the textfield[] will be a PHP array, you can use it easily then. 发布表单后, textfield[]将是一个PHP数组,然后可以轻松使用它。 When you remove an element make sure its removed from the HTML DOM. 删除元素时,请确保将其从HTML DOM中删除。

you can use POST or GET 您可以使用POST或GET

After submit you can use all of your form element with this automaticly. 提交后,您可以自动将所有表单元素与此一起使用。 You dont need to reorganise your form element names. 您不需要重新组织表单元素名称。 Even you dont need to know form elements names. 即使您不需要知道表单元素的名称。

<form method="POST" id="fr" name="fr">.....</form>

<?php
if(isset($_POST['fr'])){  
     foreach($_POST as $data){
          echo $data;
     }
}
?>

Also you should look this grafanimasyon.blogspot.com.tr/2015/02/veritabanndan-php-form-olusturucu.html 此外,您还应该查看此grafanimasyon.blogspot.com.tr/2015/02/veritabanndan-php-form-olusturucu.html

This is a automated form creator calcutating your database tables. 这是一个自动的表单创建者,负责计算数据库表。 You can see how to give name to form elements and use them. 您可以看到如何为表单元素命名并使用它们。

Like blejzz suggests, I think if you use $_GET, then you can just cycle through all of the inputs that were sent, ignoring the deleted rows. 就像blejzz建议的那样,我认为如果您使用$ _GET,那么您可以循环浏览所有已发送的输入,而忽略删除的行。

foreach ($_GET as $k=>$v) {
      echo "KEY: ".$k."; VALUE: ".$v."<BR>";
}

I notice that you mention "accounting for deleted rows"; 我注意到您提到“对已删除的行进行会计处理”; you could include a hidden input, and add a unique value to it each time someone deletes a row. 您可以包含一个隐藏的输入,并在每次有人删除行时为其添加唯一值。 For example, the input could hold comma-separated values of the row numbers: 例如,输入可以包含逗号分隔的行号值:

<input type="hidden" value="3,5,8" id="deletions" />

and include in your jQuery script: 并包含在您的jQuery脚本中:

$('.delete').click(function(){
      var num = //whatever your method for getting the row number
      var v = $('#deletions').val();
      v = v.split(',');
      v.push(num);
      v = v.join(',');
      $('#deletions').val(v);
});

Then you should be able to know which rows were deleted (if that is what you were looking for). 然后,您应该能够知道删除了哪些行(如果这是您要查找的内容)。

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

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