Getting Checked row details in a PHP array
How to obtain the checkbox checked Rows in a PHP array
In this blog we are going to discuss how to obtain the checkbox checked Rows in a PHP array. Array contains each selected row information. As shown in the image.
Here is the code for getting all checked rows of a table into an array using PHP. Follow the example.
There is an HTML table displaying all users with name and id on a table. Against each user id, there is one checkbox(dynamic). When clicked the Submit button, all the checked user details (id, name)are added to the PHP array.
Example code:
<!doctype html>
<html>
<head>
</head>
<body>
<form method="post" action="">
<span>Select Users </span><br/>
<?php
$checked_arr = array();
// Fetch checked values
$con = mysqli_connect("localhost","root","", "testdb");
$fetchUser = mysqli_query($con,"SELECT * FROM users");
if(mysqli_num_rows($fetchUser) > 0){
$result = mysqli_fetch_assoc($fetchUser);
$checked_arr = explode(",",$result['username']);
}
?>
</form>
<?php
if(isset($_POST['rows']) and !empty($_POST['users'])){
$users = $_POST['users'];
echo "<pre>";
print_r($users);
echo "</pre>";
}
else
{
echo "No rows selected";
}
?>
<form method="post" action="">
<?php
$result= mysqli_query($con,"SELECT * FROM users");
echo '<table border="1">';
while($row = mysqli_fetch_assoc($result)){
echo "<tr>";
$str = implode(",",$row);
?>
<td><input type="checkbox" name="users[]" value="<?php echo $str; ?>"><?php echo $row["id"]?> </td>
<?php
echo "<td>".$row["username"]."</td></tr>";
}
echo "</table>";
?>
<input type="submit" value="Submit" name="rows">
</form>
</body>
</html>


Comments
Post a Comment