Tsujit Pathak
Tsujit Pathak

Reputation: 77

retrieving data from checkbox using php

i have send the value dynamically from check-box and when i tried to retrieve all the value dynamically using loop, it just goes on loading. my code for sending value from check-box:

while ($row=mysql_fetch_array($query))
{

   $member_id=$row["member_id"];
   <input type='checkbox' name='check' value='$member_id'>
}

// this is working. but when i try to fetch the data from checkbox from only where the tick is given it doesn't work. this is how i tried to fetch the data


while(isset($_POST['check']))
{
   echo $_POST['check']."<br>";

}

Upvotes: 0

Views: 6623

Answers (4)

coderkane
coderkane

Reputation: 498

The trick here is, when you have multiple checkboxes with the same name and you want to get all the checked values on the server side, then you need to add [] after the name of the checkbox field in the html, eg.

<input type='checkbox' name='check[]' value='$member_id'>

if you do this, then $_POST['check'] will be an array of all the checked elements. As pointed out by others,

while(isset($_POST['check']))

represents an infinite loop. It should be

if(isset($_POST['check']))
foreach($_POST['check'] as $each_check)
 echo $each_check;

And finally, its a duplicate of an existing question. Please search before asking again :)

Upvotes: 3

veena s kurup
veena s kurup

Reputation: 151

 foreach ($_POST['check'] as $selected) {
   $selections[] = $selected;
 }
 print_r($selections);

and change your html tag as :

<input type="checkbox" name="check[]" value=".$member_id.">

Upvotes: 0

Joe
Joe

Reputation: 8272

if you want to get all the checkboxes

WRONG WILL CAUSE INFINITE LOOP

while(isset($_POST['check']))
{
    echo $_POST['check']."<br>";
}

One of the many correct alternatives:

foreach ($_POST['check'] as $val) {
     echo $val.'<br>';
}

Upvotes: 0

Jahanzeb
Jahanzeb

Reputation: 613

You added While loop, with the condition that will always true. So loop will become infinite. Change you loop to foreach, like this

foreach ($_POST['check'] as $value)
{
  echo $value."<br>";
}

AND your check-boxes will not show till then you add echo, like this

while ($row=mysql_fetch_array($query))
{

  $member_id=$row["member_id"];
  echo "<input type='checkbox' name='check' value='$member_id'>";
}

Upvotes: 0

Related Questions