Jump to content

Looping for checkbox in form


sungpeng

Recommended Posts

if($x==""){

$x=1;

}

while($do=mysql_fetch_array($sql){

<form name='Form' action='posting.php' method='post'>

<input type='checkbox' name='<?php print $x; ?>' value='<?php print $do[id]; ?>' />

<input type='submit' name='checked' value='checking'>

$x++;

}

 

 

When I click on the submit button, only one value of $x is sent to posting.php instead of multiple loop values in checkbox. Does anyone have a solution to it ?

 

Under Posting.php

for ($x 1$x <= 20$x++) {

$x=$_POST[$x];

echo "$x";

}

Link to comment
https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/
Share on other sites

if($x==""){

$x=1;

}

<form name='Form' action='posting.php' method='post'>

while($do=mysql_fetch_array($sql){

<input type='checkbox' name='<?php print $x; ?>' value='<?php print $do[id]; ?>' />

$x++;

}

<input type='submit' name='checked' value='checking'>

 

 Still the same problem

Only put in the loop the parts of form need to add new values with php

 

well you did not break out of php and back in, might as well echo in php

<form name='Form' action='posting.php' method='post'>
<?php
if($x==""){

$x=1;

}

while($do=mysql_fetch_array($sql){
echo "<input type='checkbox' name='".$x."' value='".$do[id]."' />";
$x++;
}
?>
<input type='submit' name='checked' value='checking'>
                 

Maybe this will help, i used a dummy range versus your while loop

<form name='Form' method='post'>
<?php
$dummy_array = range(1, 20);


$x = 1;

foreach ($dummy_array as $ids) {
    echo $x . ": <input type='checkbox' name='" . $x . "' value='" . $ids . "' /><br />";
    $x++;
}
?>
<input type='submit' name='checked' value='checking'><br />


<?php
//display
if (isset($_POST)) {
    foreach ($_POST as $key => $value) {
        echo $key . " - " . $value . "<br />";
    }
}
?>

The while loop is the loop.

 

and correct, just display the value in the $_POST array

 

instead of making each checkbox name a number, you can just make them an array and loop those

<form name='Form' method='post'>
<?php
$dummy_array = range(1,20);

foreach($dummy_array as $ids){
echo $ids.": <input type='checkbox' name='staff[]' value='".$ids."' /><br />";
}
?>
<input type='submit' name='checked' value='submit'><br />
<?php
//display
if(isset($_POST['staff'])){
foreach($_POST['staff'] as $staff){
echo $staff."<br />";
}
}
?>

So now I will put this together as what your code is, be sure to break in and out of php to html properly or echo the entire form in php

 

code:

<?php

echo "<form name='Form' action='posting.php' method='post'>";


while($do=mysql_fetch_array($sql){

echo $do['id'].": <input type='checkbox' name='staff[]' value='".$do['id']."' /><br />";

}

echo "<input type='submit' name='checked' value=checking'><br />";

?>

posting.php

<?php
//display on posting.php
if(isset($_POST['staff'])){
foreach($_POST['staff'] as $staff){
echo $staff."<br />";
}
}
?>

Do you have a checked/unchecked value or equivalent in your database?

This way can mark the checked ones.

 

Taking a stab in the dark here...

if($do['checked'] == true){
$checked = "checked='checked'";
}else{
$checked = "";
}
echo "<input type='checkbox' name='staff[]' value='".$do['id']."' ".$checked." />";

If you want to pass all the staff regardless to checked or not can send a hidden value.

echo "<input type='hidden' name='allstaff[]' value='".$do['id']."' />";

so on display would be $_POST['allstaff']

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.