0

I am trying to figure out how to loop through multiple POST form data that is dynamically pulled from a database and re-submit the modified data to a different table. For some reason (probably old age) I can't seem to come up with a solution that works.

I am already looping out all the records from one table (call it roster) and need to submit it to another table (call it roster2). The form is something similar to this:

    <form name="name" action="form.php" method="post">
<input type="text" name="name1" value="15">
<input type="text" name="attended" value="1">
<input type="checkbox" name="nameid_12" value="12">

<input type="text" name="name2" value="8">
<input type="text" name="attended" value="1">
<input type="checkbox" name="nameid_6" value="6">
</form>

The 'name' and 'nameid' fields will always change and the number of records displayed will always be different (one day it could be 5 and the next 100). What is the best way to loop through the POST data to submit it to the database keeping all the associations intact? I am relatively new to working with PHP and I can't seem to figure out a good way to do this.

1
  • try to use an array to post eg: <input type="text" name="name[]" value="15"> <input type="text" name="attended[]" value="1"> <input type="checkbox" name="nameid_[]" value="12"> Mar 7, 2014 at 22:37

2 Answers 2

0

If you're going to generate them dynamically, I would recommend using PHP to place the ID of each form in the inputs on that form. So your original form would end up with these names:

<input type="text" name="name[12]" value="15">
<input type="text" name="attended[12]" value="1">
<input type="checkbox" name="nameid[12]" value="12">

<input type="text" name="name[6]" value="8">
<input type="text" name="attended[6]" value="1">
<input type="checkbox" name="nameid[6]" value="6">

Then your arrays will have keys corresponding to their form's ID. The array structure looks like this.

Array ( 
    [name] => Array ( [12] => 15, [6] => 8 ) 
    [attended] => Array ( [12] => 1, [6] => 1 ) 
    [nameid] => Array ( [12] => 12 [6] => 6 )
)

Now we need to figure out which ids are actually present today. The array_keys() function generates an array of keys from any source array. Keys will be the same for each of the three elements, so I arbitrarily take the keys from [name].

$id_array = array_keys($_POST['name']);

Then, to access each element of the POST array, we'll use a foreach.

foreach ($id_array as $id) {

    //assign variables
    $name = $_POST['name'][$id];
    $attended = $_POST['attended'][$id];
    $nameid = $_POST['nameid'][$id];

   //store
   //Using whichever database style you like. I prefer PDO.
}
1
  • I haven't tried this yet but I believe it is exactly what I have been looking for... Thank you Mar 8, 2014 at 0:58
0

You can loop through the fields and retain their keys like so:

foreach ($_POST as $field_name => $field_value) {
    // Storage
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.