使用php在HTML表格复选框中循环

使用php在HTML表格复选框中循环,php,Php,我有一个使用javascript的类似线程,但我发现了一些问题和bug。所以我改用php 到目前为止,这是我的代码 if(!empty($_POST['check'])) { foreach($_POST['check'] as $check) { //get the html data beside the checked checkboxes } }else{ echo '<script language="javascript

我有一个使用javascript的类似线程,但我发现了一些问题和bug。所以我改用php

到目前为止,这是我的代码

if(!empty($_POST['check'])) {
    foreach($_POST['check'] as $check) {
        //get the html data beside the checked checkboxes

    }
    }else{
        echo '<script language="javascript">';
        echo 'alert("Nothing checked")';
        echo '</script>'; 
    }
还有我的html代码

<tr>
                                <td><?php echo $r['Name'] ?></td>
                                <td><?php echo $r['Age'] ?></td>
                                <td><?php echo $r['Address'] ?></td>

                                <td><input name="check[]" type="checkbox"  ></td>
                            </tr>

我不想在选中复选框的同一行上获取数据。请帮帮我。谢谢

您必须循环查看POST数据,并分配$r[NAME]=VALUE

对于PHP代码:

    array $r;
    foreach($_POST as $post=>$value) {
        //get the html data beside the checked checkboxes
        $r[ $post] = $value;
        } 
    }
    }else{
        echo '<script language="javascript">';
        echo 'alert("Nothing checked")';
        echo '</script>'; 
    }
假设您引用的HTML部分是结果页面 对于HTML:

<tr>
                                <td><?php echo $r['Name'] ?></td>
                                <td><?php echo $r['Age'] ?></td>
                                <td><?php echo $r['Address'] ?></td>

                            </tr>

首先,让我们添加一些数据:

<?php

$data = [
  '111' => [
    'Name' => 'John',
    'Age' => '25',
    'Address' => 'Baker street, 11'
  ],
  '222' => [
    'Name' => 'Mary',
    'Age' => '19',
    'Address' => 'Elm street, 14'
  ],
  '333' => [
    'Name' => 'Nick',
    'Age' => '23',
    'Address' => '64th stree, 2'
  ]
];

?>
输出所有数据项并根据以前发布的数据设置检查:

<form method="POST">
<table>
  <?php foreach($data as $id => $item): ?>
  <tr>
    <td><?= $item['Name'] ?></td>
    <td><?= $item['Age'] ?></td>
    <td><?= $item['Address'] ?></td>
    <td><input name="check[]" type="checkbox" value="<?= $id; ?>"
      <?php if (isset($_POST['check']) && in_array($id, $_POST['check'])): ?>
      checked="checked"
      <?php endif; ?>/></td>
  </tr>
  <?php endforeach; ?>
</table>
<input type="submit" />
</form>
<?php if (empty($_POST['check'])): ?>
<script>alert("Nothing checked");</script>
<?php endif; ?>

提交表格后,您将重新加载带有正确标记的复选框的页面。

先生,您能详细说明一下吗?或者准确地说。使用php获取html表的数据需要什么代码。请解释清楚,先生。英语不是我的语言。谢谢,我尝试了上面的代码,但它不起作用。您能更新OP以显示所需的结果吗?先生,我们要做的是在选中复选框的同一行上获取数据。@knowmeifyou您在那里找到了它。您能给我看一下JSFIDLE sir中的示例代码以解决我的问题吗。我真的不明白。例如,php是一种服务器端语言,创建一个test index.php文件,将整个代码复制到其中,并通过浏览器请求页面,我对代码的理解会更好。