【问题标题】:retrieving array values from a checkbox php从复选框php中检索数组值
【发布时间】:2013-05-13 21:21:28
【问题描述】:

我很难看出这里有什么问题。我正在尝试根据已选中的复选框从我的数据库中删除。 if(!empty($_POST['removeCheckbox'])) 说 removeCheckbox 是空的,即使我回显 $currentRow[] 它显示值。提前致谢。

while($row = mysqli_fetch_array($paperToReview)){    
    if ($row > 0) {
        $currentRow = array($row['uid'], $row['toReview']);
                echo $currentRow[0];
                echo $currentRow[1];            
        echo '<td width = 200><input type="checkbox" name="removeCheckbox[]" value="'.$currentRow.'"> Remove</td>';
    }
}
//Remove button
echo'<td width = 200><td width = 200><td width = 200><td width = 200><td width = 200><input type=submit name=submit value="Remove Checked Rows"></td>';

然后在这里我查看删除按钮是否已被选中并查看所有选中的框。

    if (isset($_POST['submit']) && $_POST['submit'] =="Remove Checked Rows"){
        if(!empty($_POST['removeCheckbox'])) {
        //deletes row from database
            foreach($_POST['removeCheckbox'] as $check) {
                    DBSubmit("DELETE FROM paper_review WHERE paper_reviewer_id = '" . $check[0] ."' AND paperid = '" . $check[0] ."'");

                }
        }

【问题讨论】:

    标签: php arrays checkbox


    【解决方案1】:

    你确定,符合条件吗?

    检查 $_POST 变量中的值是否正确,在提交条件之前简单地写下这样的内容

    echo(<pre>); print_r($_POST); echo(</pre>);
    

    请发送更多代码(如何创建表单,post 变量中的内容)

    T.

    【讨论】:

    • 提交条件满足了,我之前有一个header(redirect())在下面看看是不是在工作
    • 实际上 if(!empty($_POST['removeCheckbox'])) 检查不起作用。我会尝试解决这个问题
    【解决方案2】:

    这将使所有值都为“数组”,因为您不能直接回显数组 试试这个:

    while($row = mysqli_fetch_array($paperToReview)){    
        if ($row > 0) {
            $currentRow = array($row['uid'], $row['toReview']);         
            echo '<td width = 200><input type="checkbox" name="removeCheckbox[]" value="'.json_encode($currentRow).'"> Remove</td>';
        }
    }
    //Remove button
    echo'<td width = 200><td width = 200><td width = 200><td width = 200><td width = 200><input   type=submit name=submit value="Remove Checked Rows"></td>';
    

    在控制器代码中

        if (isset($_POST['submit']) && $_POST['submit'] =="Remove Checked Rows"){
        if(!empty($_POST['removeCheckbox'])) {
        //deletes row from database
            foreach($_POST['removeCheckbox'] as $jsonCheck) {
                    $check = json_decode($jsonCheck);
                    DBSubmit("DELETE FROM paper_review WHERE paper_reviewer_id = '" . $check[0] ."' AND paperid = '" . $check[0] ."'");
    
                }
        }
    

    您可以将 json_encode/decode 替换为 serialize/unserialize 或任何其他方法

    【讨论】:

    • if(!empty($_POST['removeCheckbox'])) 不起作用。我在复选框前回显了 $currentRow[0] 以确保数据库中有值并且它们按预期显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 2016-10-01
    相关资源
    最近更新 更多