枚举PHP中的所有复选框

Yog*_*007 1 php

我有PHP的网页,它显示表中的所有记录.我想为所有行添加复选框,用户可以勾选复选框以选择行,然后提交页面.提交页面时,我想枚举所有复选框并检查它们是否被选中,我该怎么做?

dec*_*eze 5

您将创建如下所示的复选框:

<input name="rows[]" value="uniqueIdForThisRow" type="checkbox" />
<input name="rows[]" value="anotherId" type="checkbox" />
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样循环遍历它们:

<?php
    // $_POST['rows'] contains the values of all checked checkboxes, like:
    // array('uniqueIdForThisRow', 'anotherId', ...)

    foreach ($_POST['rows'] as $row) {
        if ($row == 'uniqueIdForThisRow') {
            // do something
        }
    }
?>
Run Code Online (Sandbox Code Playgroud)

有关处理表单的PHP文档,请参见示例#3.