2013-05-13 87 views
0

我很难看到这里出了什么问题。我正在尝试从我的数据库中删除基于哪些复选框已被选中。 if(!empty($ _ POST ['removeCheckbox']))表示removeCheckbox是空的,即使当我回显$ currentRow []时它显示值。提前致谢。从复选框中检索数组值php

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] ."'"); 

       } 
     } 

回答

0

你确定满足条件?

检查,如果你有$ _ POST变量,简单写这样的事情,正确的价值观您提交前条件

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

请发送更多的代码(你如何创建一个表单,什么是后变量)

T.

+0

,我以前有下一个报头(重定向())在那里看到它正在工作 – 2013-05-13 21:30:25

+0

实际上if(!empty($ _ POST ['removeCheckbox']))检查不起作用。我会尽力解决这个问题 – 2013-05-13 21:34:45

0

这将使所有值“阵”,因为你不能回声阵列直接 试试这个:

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] ."'"); 

      } 
    } 

可以替换的serialize json_encode /解码/反序列化或提交条件被满足的任何其它方法

+0

if(!empty($ _ POST ['removeCheckbox']))虽然不起作用。我在复选框之前回显$ currentRow [0]以确保数据库中有值,并且它们按预期显示。 – 2013-05-13 21:44:29