简体   繁体   中英

Extract dynamically created form data

I've just started using 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 . I increment rowNumber each time I add a row.

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.

When the form is submitted, I set a hidden element to equal the rowNumber value from jQuery. Then in PHP, I count from 1 to the rowNumber value. Then for each value, I perform an isset($_REQUEST['name'_ . index]) . This is how I extract the form elements that remained after deleting rows in 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.

It does add some complexity in that 'name[0]' has to assume 'detail[0]' is the correct element.

PHP will create an array and append elements if the field name ends with [] similar to

<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[] . This means that $_POST['name'] will be an array.

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. When you remove an element make sure its removed from the HTML DOM.

you can use POST or 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

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.

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:

$('.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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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