0

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>";

}
4
  • 1
    After editing your question I am worried a lot. Is that what you have in your php file. I am talking about the while loops code. Commented Aug 14, 2013 at 10:49
  • 2
    "While $_POST['check'] is set, output it, then repeat"... How is this not an obvious infinite loop? Did you mean if instead of while? Commented Aug 14, 2013 at 10:50
  • dup: stackoverflow.com/questions/7654155/… Commented Aug 14, 2013 at 10:53
  • using if loop will only take one chekbox value but i need multiple. what can i do now? Commented Aug 14, 2013 at 11:15

4 Answers 4

3

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 :)

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

Comments

0

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'>";
}

Comments

0

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>';
}

Comments

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

and change your html tag as :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.