简体   繁体   English

如何在 foreach 循环 PHP 中创建和命名复选框

[英]How do I create and name checkboxes within a foreach loop PHP

I seem to be a little stuck with this.我似乎有点坚持这一点。

I am trying to create checkboxes within a table of data within a foreach loop.我正在尝试在 foreach 循环内的数据表中创建复选框。

    foreach($datas as $data){
      echo "<tr>";
      echo "<td><input type='checkbox' name='counter[]'></input></td>";
      echo "<td>" .$data['employeeName']. "</td>";
      echo "<td>" .$data['amount']. "</td>";
      echo "<td>" .$data['reason']. "</td>";
    }

This will print out a table of employee reimbursements that have yet to be reimbursed.这将打印出尚未报销的员工报销表。 From here I am wanting to be able to have the user check the checkboxes in multiple rows, and run a SQL database UPDATE statement to update the date of reimbursement in the database.从这里我希望能够让用户检查多行中的复选框,并运行 SQL 数据库更新语句来更新数据库中的报销日期。

I'm really confused on how I would name the checkboxes, and submit them to the following page.我真的很困惑如何命名复选框,并将它们提交到下一页。 Thank you in advance for your help, and please let me know if you need any additional information!提前感谢您的帮助,如果您需要任何其他信息,请告诉我!

You will have to give the employee id as the value of the checkbox您必须将员工 ID 作为复选框的值

<form name="employee" method="POST">
        foreach($datas as $data)
        {
          echo "<tr>";
          echo "<td><input type='checkbox' name='counter[]' value=$data['id']></input></td>";
          echo "<td>" .$data['employeeName']. "</td>";
          echo "<td>" .$data['amount']. "</td>";
          echo "<td>" .$data['reason']. "</td>";
        }
<button type="submit"  value="Submit">Submit</button>
</form>

When You Submit the form you will get all employees id checked in the form.当您提交表格时,您将在表格中检查所有员工 ID。

$emloyee=$_POST['counter'];

You will get the data as array.您将以数组的形式获取数据。

Then for each employeeid you can update the date of reimbursement.然后,对于每个员工 ID,您可以更新报销日期。

Hope this helps.希望这可以帮助。

You need to pass value of employee id in checkbox;您需要在复选框中传递员工 ID 的值; so when you select checkbox and submit form, you will get all checked employee id's in post data.因此,当您选择 select 复选框并提交表单时,您将在帖子数据中获得所有已检查的员工 ID。 Here is complete code that may be useful for you to go through.这是完整的代码,可能对您有用到 go 通过。

// On form post this code will be executed
if (isset($_POST['submit'])) {
    // this will give you all employee id which you have selected in array
    // it will be blank if you have not selected any employee
    $counter = isset($_POST['counter']) && !empty($_POST['counter']) ? $_POST['counter'] : [];

    // update statement here 
    if (!empty($counter)) {
        foreach ($counter as $key => $empId) {
            // update statement goes here
            // you can update statement based on $empId here
        }
    }   
}

// sample data; make sure you have employee id that you can use in update statement later on
$datas = [
    ['id' => 1, 'employeeName' => 'John', 'amount' => 100, 'reason' => 'reason 1'],
    ['id' => 2, 'employeeName' => 'Heily', 'amount' => 200, 'reason' => 'reason 2'],
    ['id' => 3, 'employeeName' => 'Keith', 'amount' => 150, 'reason' => 'reason 3']
];

// Your form
echo "<form method='post'>";
echo "<table>";
foreach($datas as $data){
    echo "<tr>";
    echo "<td><input type='checkbox' name='counter[]' value='".$data['id']."'></input></td>"; // added employee id as value here 
    echo "<td>" .$data['employeeName']. "</td>";
    echo "<td>" .$data['amount']. "</td>";
    echo "<td>" .$data['reason']. "</td>";
}
echo "</table>";
echo "<input type='submit' name='submit'>";
echo "<form>";

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

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