簡體   English   中英

如何檢查選中了哪個復選框?

[英]How to check which checkbox is ticked?

我目前正在創建一個php Web應用程序。 在我的頁面之一(“ pushnotification.php ”)中,我正在顯示一個表,該表從mysql數據庫加載一些信息並將其顯示在頁面上。 該表將在表中加載客戶端(主鍵),名稱和姓氏。 我還在表中的接收消息表中添加了另一列,該列由復選框組成。 頁面中的表如下所示:

在此處輸入圖片說明

創建表的源代碼如下:

function user_clients_table() {

   $con = mysql_connect("localhost","root",'');
   if(!$con){

   die("Cannot Connect" . mysql_error());

   }
    mysql_select_db("client_app",$con);
    $get_user_clients = "SELECT `ID`,`Name`,`SurName` FROM `clients`  ";
    $clients = mysql_query($get_user_clients,$con);

   echo "<table  border=2>
   <tr>   
   <th>Client</th>
   <th>Name</th>
   <th>SurName</th>
   <th>Receive Message</th>
   </tr>";
   while($record = mysql_fetch_array($clients)){
    echo "<form action=pushnotification.php method=post>";
    echo "<tr>";
    echo "<td>".$record['ID']." </td>";
    echo "<td>".$record['Name']." </td>";
    echo "<td>".$record['SurName']." </td>";
    echo "<td>"."<input type=checkbox name=checkbox[".$record['ID']."] </td>";   
    echo "</tr>";
    echo "</form>";
     }

echo "</table>";     
mysql_close();

}

用戶單擊陣列列表中網站的快捷鍵右側的“發送”按鈕后,如何保存被選中以接收消息的客戶端。 “我只想將主鍵保存在數組列表中,別無其他”

誰能指導我? 在此致謝

if (isset($_POST['checkbox']))
{
    foreach ($_POST['checkbox'] as $key => $value)
    {
        $receivemsg[] = $key;
    }
}

之后,您將擁有一個包含ID的數組( $receivemsg ),該ID的復選框已被選中。

更新:一個示例數組
以下是來自3個復選框的數組示例var_dump() 此示例中僅選中了復選框2和3。

array(2) {
  [0]=>
  int(2)
  [1]=>
  int(3)
}

要保存“接收消息”值,請在數據庫表中使用所需名稱創建一些字段,如果客戶端Recieve Message復選框”,則將其設置為1否則將其設置為0 從數據庫中獲取數據時,請檢查該屬性的值是否為1然后echo "<td>"."<input type='checkbox' checked>." </td>"; echo "<td>"."<input type='checkbox' checked>." </td>";否則僅echo "<td>"."<input type='checkbox'>." </td>"; 假設您的屬性名稱為rec_msg然后執行以下操作:

if($record['rec_msg'] == '1') echo "<td>"."<input type='checkbox' checked>." </td>";
else echo "<td>"."<input type='checkbox'>." </td>";

然后您的while循環將如下所示

while($record = mysql_fetch_array($clients)){
  echo "<form action=pushnotification.php method=post>";
  echo "<tr>";
  echo "<td>".$record['ID']." </td>";
  echo "<td>".$record['Name']." </td>";
  echo "<td>".$record['SurName']." </td>";
  if($record['rec_msg'] == '1') echo "<td>"."<input type='checkbox' checked>." </td>";
  else echo "<td>"."<input type='checkbox'>." </td>";  
  echo "</tr>";
  echo "</form>";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM