Site icon T4Tutorials.com

PHP Search with Checkbox

PHP Search with Checkbox

1st step: Make Database “checkboxes”.

2nd step: Add Table “tblx”

3rd step: Add Columns “id, city, country”

Database of PHP Search with Checkbox

PHP Code of PHP Search with Checkbox

Make two files, “index.php” and “select.php”.

Index.php code

<?php
$conn = mysqli_connect('localhost','root','','checkboxes');
$sql= mysqli_query($conn,'SELECT * FROM tblx');
?>
<form Method="post" Action="select.php" >
<?php 
while($result=mysqli_fetch_assoc($sql))
{
?>
      <tr>
        <td width="30%" align="center">
          <br>
        <input name="<?php echo $result['id']?>" type="checkbox" id="<?php echo $result['id']?>" value="<?php echo $result['id']?>">
        </label></td>
        <td width="30%" align="center"><?php echo $result['city']?></td>
        <td width="30%" align="center"><?php echo $result['country']?></td>
      </tr>
<?php
}
?>
      <tr>
        <br>
        <td align="center"><input type="submit" name="submit" value="FindID" /></td>
      </tr>
    </table>
  </form>

select.php Code:

<?php
$conn = mysqli_connect('localhost','root','','checkboxes');
$sql = mysqli_query($conn,'SELECT * FROM tblx');
$x = NULL;
while($fetch = mysqli_fetch_assoc($sql))
{
    $x++;
    if(isset($_POST[$x]))
    {
        echo " ".$_POST[$x]." ";
        $sql1 = mysqli_query($conn,"SELECT `city` FROM `tblx` WHERE id=$x");
        while($result = mysqli_fetch_array($sql1))
		{
			echo $result['city']."<br>";
		}
    }
}
?>

Output

index.php output:

select.php output:

Download Code – Search with Checkbox PHP

 

Exit mobile version