【问题标题】:handling checked checkboxes PHP处理选中的复选框 PHP
【发布时间】:2011-03-31 01:33:18
【问题描述】:

我有一个从数据库中获取数据的表,如下所示:(不是表单)

if (mysql_num_rows($result)) {
        echo "<table id='logs' border='1' cellspacing='0' width='62%'>";
        echo "<tr>";
        echo "<th width='15%'>Time Registered</th>";
        echo "<th width='15%'>Username</th>";
        echo "<th width='15%'>Password</th>";
        echo "<th width='15%'>IP Address</th>";
        echo "<th width='2%'><a href=\"#\" onclick=\"checkAll(this);\">Mark</a></th>";
        echo "<th width='2%'>Delete</th>";

        echo "</tr>";
        while ($row = mysql_fetch_row($result))
        {
            echo "<tr>";
            echo ("<p><td>$row[2]</td><td>$row[0]</td><td>$row[1]</td><td><i>$row[3]</i></td><td><center><input type=\"checkbox\" name=\"mark[]\"/></center></td><td><a href=\"delete.php?time=$row[2]&user=$row[0]&pass=$row[1]&ip=$row[3]\"><center>[x]</center></a></td></p>");
            echo "</tr>";
        }
        echo "</table>";
}

&lt;input type=\"checkbox\" name=\"mark[]\"/&gt; 部分是复选框。如何找到并处理选中的复选框?

if(mark[$checked]) {
     //delete data from database if row checked
}

【问题讨论】:

    标签: php checkbox


    【解决方案1】:
    foreach($_REQUEST['mark'] as $value){
        echo "$value was selected\n <br />";
    }
    

    如果你想知道哪个没有被选中,那么将所有可能的选择存储到一个数组中,然后遍历这个数组并执行类似

    的操作
    foreach($poss_select as $key=>$val){
        if(!in_array($val,$_REQUEST['mark']){
            $not_selected[$key] = $value;
        }else{
            deleteRow($value);
        }
    } 
    

    【讨论】:

      【解决方案2】:

      $_REQUEST[ 'mark' ] 将是所有选中框的数组。

      【讨论】:

        【解决方案3】:

        您需要执行一些 AJAX 或表单提交以将该复选框数据传递给 PHP 进行处理。

        但在您这样做之前,您需要在这些复选框上包含一个 value 属性。目前,$_POST['mark'](如果您使用 POST 提交表单)将是一个基于 0 的已选中复选框数组(并且只有已选中的复选框)。

        我建议将用户 ID 作为复选框的值输出,以帮助识别标记的用户。

        那你就可以了

        foreach ($_POST['mark'] as $marked) {
            // $marked would contain the value attribute of the checked checkboxes
            // and you could run a SQL query for each value of $marked.
        }
        

        【讨论】:

          猜你喜欢
          • 2021-10-05
          • 2011-10-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-25
          • 1970-01-01
          • 2013-01-06
          • 1970-01-01
          相关资源
          最近更新 更多