简体   繁体   中英

Get the values of the ticked checkboxes in php

I am currently building a web application. In my application, a load some data from mysql and I display them as a table in my website. Additionally I add another column that consists of different checkboxes. My source code of displaying the table is called by a function that is located in another page. The source code odf the function 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>ID</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[] value=".$record['ID']." />".  "</td>"; 
    echo "</tr>";
    echo "</form>";
     }

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

}

The function works fine, after i call the function my webpage looks like this:

在此处输入图片说明

I want next to display the client number whose check box has been checked after i click the button send. For example if i checked only the first check box and submit it, i want to echo the client id that matches thsi checkbox, in this case i will echo '2'. My approach to this is the following:

if(isset($_POST['send'])){
if(!empty($_POST['checkbox'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['checkbox']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach($_POST['checkbox'] as $selected) {
echo "<p>".$selected ."</p>";
}
echo "<br/><b>Note :</b> <span>Similarily, You Can Also Perform CRUD Operations using These Selected Values.</span>";
}
else{
echo "<b>Please Select Atleast One Option.</b>";
}
}

It works but only for the first checkbox, if I select the other checkboxes without the first one I doesn't display anything.

Can someone please help me?

Thanks in Regards

Each checkbox is in it's own form, so when you are submitting you are submitting the first form (the first checkbox), and that is why you are getting the current action. Put the form tags outside the loop

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