从代码点火器中的 foreach 中的输入文本中获取值



我有一个关于如何从foreach()中的输入文本中获取值的问题,当我执行代码时,它会获取表中的最后一行数据。

下面是视图代码:

<?php
$count = 1;
foreach ($data as $row) {?>
<thead> 
  <th>No</th>
  <th>Out ID</th>
  <th>Name</th>
  <th>Dorm</th>
  <th>Date</th>
  <th>Approve</th>
  <th>Comment</th>
  <th>Action</th>
</thead>
<tbody>
<tr>                                                      
    <td><center><?php echo $count; ?> </td>
    <td><center><?php echo $row->IdOut; ?> </td>
    <td><center><?php echo $row->Name; ?> </td>
    <td><center><?php echo $row->Dorm; ?> </td> 
    <td><center><?php echo $row->Date; ?> </td>
    <td><center><input type="checkbox" name="approvechk"></td>
    <td><center><input type="text" name="aproverComment"></td>
    <td><center><input type="submit" name="submit" value"submit"></td>
</tr> 
</tbody>
<?php $count ++;
}
?>

单击提交时,如何从表中获取选中的行和输入?

您必须对代码进行一些更改,例如:

<center><input type="checkbox" name="approvechk">

上面的 CheckBox 语句驻留在 foreach() 循环中,因此您必须更改其名称,如下所示:

<center><input type="checkbox" name="approvechk[]">

这里使用了approvechk[],以便它可以在其中保存多个值。

之后,整个输入元素不会包装在表单标签中,因此无法将其直接提交到服务器。因此,要么包装在<form>标签中,要么使用 jquery 将用户数据提交到服务器。

下面的代码可以工作,但如果你看看数据表会更好,它更适合处理表中的数据。

    <?php
    $count = 1;
    foreach ($data as $row) {?>
    <thead> 
    <th>No</th>
    <th>Out ID</th>
    <th>Name</th>
    <th>Dorm</th>
    <th>Date</th>
    <th>Approve</th>
    <th>Comment</th>
    <th>Action</th>
    </thead>
    <tbody>
    <tr>                                                      
        <td><center><?php echo $count; ?> </td>
        <td><center><?php echo $row->IdOut; ?> </td>
        <td><center><?php echo $row->Name; ?> </td>
        <td><center><?php echo $row->Dorm; ?> </td> 
        <td><center><?php echo $row->Date; ?> </td>
<form type="post">
        <td><center><input type="checkbox" name="approvechk"></td>
        <td><center><input type="text" name="aproverComment"></td>
        <td><center><input type="submit" name="submit" value"submit"></td>
</form>
    </tr> 
    </tbody>
    <?php $count ++;
    }
    ?>
  <thead> 
    <th>No</th>
    <th>Out ID</th>
    <th>Name</th>
    <th>Dorm</th>
    <th>Date</th>
    <th>Approve</th>
    <th>Comment</th>
    <th>Action</th>
    </thead>
    <tbody>
 <?php
    $count = 1;
    foreach ($data as $row) { ?>
    <tr>                                                      
        <td><center><?php echo $count; ?> </td>
        <td><center><?php echo $row->IdOut; ?> </td>
        <td><center><?php echo $row->Name; ?> </td>
        <td><center><?php echo $row->Dorm; ?> </td> 
        <td><center><?php echo $row->Date; ?> </td>
        <td><center><input type="checkbox" name="approvechk"></td>
        <td><center><input type="text" name="aproverComment"></td>
        <td><center><input type="submit" name="submit" value"submit"></td>
    </tr> 
<?php $count ++;
    }?
    </tbody>

>试试这个方法,你不需要在foreach里面包含整个表,只需要行((

最新更新