2011-03-30 76 views
1

我有一個表從數據庫中,像這樣需要數據:(是不是一種形式)處理選中的複選框PHP

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>"; 
} 

的部分<input type=\"checkbox\" name=\"mark[]\"/>是複選框。我怎樣才能找到並處理選中的複選框?

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

回答

3
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' ]將是一個數組所有的檢查框。

0

您需要做一些AJAX或表單提交以將該複選框數據傳遞給PHP進行處理。

但是在你做這件事之前,你需要在這些複選框中包含一個值屬性。目前,$ _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. 
} 
+0

所以我需要添加一個形式得到它的工作? – Kyle 2011-03-30 18:04:53