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?

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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); 
        }
    }
?>

3 Comments

Check it now @AshneelChand.
so the ` $id` is stored as numbers right so when i do WHERE id = $id it will say WHERE id = '1' ` but it should be somthing like WHERE id = jack
Yes id is a integer, and no need to use Quotation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.