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

}

Edited by sungpeng
Link to comment
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

Link to comment
Share on other sites

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'>
                 
Edited by QuickOldCar
Link to comment
Share on other sites

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 />";
    }
}
?>
Link to comment
Share on other sites

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 />";
}
}
?>
Edited by QuickOldCar
Link to comment
Share on other sites

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 />";
}
}
?>
Link to comment
Share on other sites

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']

Edited by QuickOldCar
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.