2

I have a check box inside a while loop like this:

 <form method="POST">
    <?php $sql= mysql_query("SELECT * FROM names WHERE `id` ='$id' ");

     while ($get = mysql_fetch_array($sql)){ ?>
    <input type="checkbox" name="id_names" value="<? echo $get ['id'];?>"><?php echo $get ['name']; ?>
    <?php } ?>
   <input id="submitbtn"  type="submit" value="Submit" /><br><br>
</form>

The problem is at this part I am unable to get specific checkbox properties and even if the user selects two check boxes I am unable to echo the id out

<?php
      if(isset($_POST['id_names']))
        {
         $id_names= $_POST['id_names'];
         $email = mysql_query("SELECT `email` FROM users WHERE `id` = '$id_names'  ");
  while ($getemail = mysql_fetch_array($email))
      {
        echo $getemail['email'];
      }
         }
    ?>

I have tried searching for answers but I am unable to understand them. Is there a simple way to do this?

JSON C11
  • 11,272
  • 7
  • 78
  • 65

2 Answers2

0

The form name name="id_names" needs to be an array to allow the parameter to carry more than one value: name="id_names[]".

$_POST['id_names'] will now be an array of all the posted values.

Egg
  • 1,782
  • 1
  • 12
  • 28
  • how can we differentiate that array suppose, I get `$_POST['id_name'] = 'add_2,add_3,add_4,add_5'`, so how can I get different different. – Shurvir Mori Jan 31 '20 at 15:32
0

Here your input field is multiple so you have to use name attribute as a array:

FYI: You are using mysql that is deprecated you should use mysqli/pdo.

<form method="POST" action="test.php">
    <?php $sql= mysql_query("SELECT * FROM names WHERE `id` =$id ");

    while ($get = mysql_fetch_array($sql)){ ?>
        <input type="checkbox" name="id_names[]" value="<?php echo $get['id'];?>"><?php echo $get['name']; ?>
        <input type="checkbox" name="id_names[]" value="<?php echo $get['id'];?>"><?php echo $get['name']; ?>
    <?php } ?>
    <input id="submitbtn"  type="submit" value="Submit" /><br><br>
</form>

Form action: test.php (If your query is okay.)

<?php
    if(isset($_POST['id_names'])){
        foreach ($_POST['id_names'] as $id) {
            $email = mysql_query("SELECT `email` FROM users WHERE `id` = $id");
            $getemail = mysql_fetch_array($email); //Here always data will single so no need while loop
            print_r($getemail); 
        }
    }
?>
AHJeebon
  • 1,218
  • 1
  • 12
  • 17