Best way to handle dynamic amount of form fields in PHP?

烂漫一生 提交于 2019-12-03 10:06:17

问题


I have a system where I need to list an arbitrary amount of employees with a textfield for each day of the week where an "hours worked" value can be entered.

So I need to generate a table with a dynamic number of rows, and each row is going to contain 7 text fields. I'm just wondering what is the best convention to use when assigning ID's to these fields to make it easy to iterate over once I receive the input data on my back end?

Each row will have an ID number associated with the row that represents the employee's ID.

It would be awesome to be able to do something like:

foreach($rows as $row)
{
     $id = $row['id'];

     $employee = Employee::find($id);

     foreach($row['hoursWorked'] as $dailyHours)
     {
           $timecard = new Timecard();
           $timecard->hours = $dailyHours;
           $employee->timecards->insert($timecard);
     }
}

What's the best way to structure my form and ID my inputs on the HTML side to make this as painless as possible?

As a sidenote, I'm working within the Laravel 3 framework in case that opens up any other solutions.


回答1:


<input type="text" name="hoursWorked[]" /> will internally convert to an array under $_POST['hoursWorked']. That means you can do something like this:

<input type="text" name="hoursWorked[12345][]" /> <!-- Sunday -->
<input type="text" name="hoursWorked[12345][]" /> <!-- Monday -->
<input type="text" name="hoursWorked[12345][]" /> <!-- Tuesday -->
<input type="text" name="hoursWorked[12345][]" /> <!-- Wednesday -->
<input type="text" name="hoursWorked[12345][]" /> <!-- Thursday -->
<input type="text" name="hoursWorked[12345][]" /> <!-- Friday -->
<input type="text" name="hoursWorked[12345][]" /> <!-- Saturday -->

Then, in PHP:

<?php
foreach ($_POST['hoursWorked'] as $employeeId=>$dayArray) {
    foreach ($dayArray as $dayOfWeek=>$hoursWorked) {
        // $employeeId will be 12345
        // $dayOfWeek will be 0, 1, 2, 3, 4, 5 ,6
        // $hoursWorked will be the value of the text field
    }
}



回答2:


I've never used the Laravel framework, but in general I do this in PHP:

foreach ($employee as $key=>$e) {
   echo '<input type="text" name="hours[]" id="hours_'.$key.'" value="'.$e.'" />';
}

This way you will have an array of hours values in the POST, and you can reference the corresponding field by id if you need to. The first field will have id="hours_1", etc. Alternately, if you don't want to use the $key from the query, you can do this:

$cntr = 1;
foreach ($employee as $e) {
   echo '<input type="text" name="hours[]" id="hours_'.$cntr.'" value="'.$e.'" />';
   $cntr++;
}

When you capture the values in the POST, you will have an array of values in $_POST['hours']. Remember that it is a zero based array, but you can use a foreach loop to iterate through the values.



来源:https://stackoverflow.com/questions/15819920/best-way-to-handle-dynamic-amount-of-form-fields-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!