简体   繁体   中英

How to check which checkbox is ticked?

I am currently creating a php web application. In one of my pages (" pushnotification.php ") I am displaying a table that loads some information from mysql database and display them on the page. The table loads the Clients(primary key), name and surname in the table. I have also added another column in the table called Receive Message which consists of checkboxes. The table in the page looks like this:

在此处输入图片说明

The source code that creates the table is the following:

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();

}

How can i save the Clients that have been ticked to receive a message after the user clicks the send button in the bootom right of the website in an array list. "I just want to save the primary key in an array list nothing else"

Can anyone please guide me? Thanks in Regards

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

After this, you'll have an array ( $receivemsg ) containing ID s which have the checkbox ticked.

Update: A sample array
Below is a sample var_dump() of the array from 3 checkboxes. Only checkbox 2 and 3 is checked in this sample.

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

To save the Recieve Message value create some field in your Database Table with your desired name, if the client checks the Recieve Message Checkbox set it to 1 else set it to 0 . While fetching the data from Database check if the the value of that attribute is 1 then echo "<td>"."<input type='checkbox' checked>." </td>"; echo "<td>"."<input type='checkbox' checked>." </td>"; else only echo "<td>"."<input type='checkbox'>." </td>"; . Let's say your attribute name is rec_msg then do something like this:

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

then your while loop will look like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM