Lamez Posted February 4, 2008 Share Posted February 4, 2008 How do I handle checkboxes in PHP? Say I select 3 boxs out of 4, how do I get it to display those three checkboxs? here is my php code I have so far: (does not work) <?php if(isset($_POST['Submit'])) { for ($i=0; $i<count($_POST['checkbox']);$i++) { echo "<br />value $i = ".$_POST['checkbox'][$i]; } } ?> here is my html <?php include ("../../style/include/session.php"); include ("../../style/include/cons/head_2.php"); print '<div class="box"><h2>Select Paid</h2>'; if($session->isAdmin()){ ?> <form action="pay_do.php" method="post"> <table width="100%" border="0"> <tr> <td width="419"><strong>First Name </strong></td> <td width="487"><strong>Last Name </strong></td> <td width="257"><strong>Paid (username) </strong></td> </tr> <? $qr=mysql_query("select * from users order by first"); while ($rs=mysql_fetch_assoc($qr)) { ?> <tr> <td><? echo $rs['first'];?></td> <td><? echo $rs['last'];?></td> <td><input type="checkbox" name="checkbox[]" value="<?=$rs['username'] ?>" /> (<? echo $rs['username']; ?>)</td> <? } ?> </tr> </table> <br /> <input name="submit" type="submit" value="Mark Paid" /> </form> <?php }else{ header("Location: ../../index.php"); } print '</div>'; include ("../../style/include/cons/foot.php"); ?> -Thanks! Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 a little improvement on what i wrote earlier, what are you trying to do now still put the checked boxes into the database? Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 4, 2008 Author Share Posted February 4, 2008 lol ya. If I could just learn how to handle checkboxes then I think I can get the rest Quote Link to comment Share on other sites More sharing options...
tibberous Posted February 4, 2008 Share Posted February 4, 2008 Do a print_r($_POST) and see what you are getting. It's like no one traces anything anymore. Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 4, 2008 Author Share Posted February 4, 2008 when I select a few boxes I get this Array ( [checkbox] => Array ( [0] => GoogleBot [1] => admin ) [submit] => Mark Paid ) Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 <?php if(isset($_POST['Submit'])) { $boxes=$_POST['checkbox']; for ($i=0; $i<count($boxes);$i++) { mysql_query("insert into paid (user) values ('$boxes')"); } } ?> Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted February 4, 2008 Share Posted February 4, 2008 just do foreach() Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 4, 2008 Author Share Posted February 4, 2008 mike that loop did not work, how would I use the foreach? Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 4, 2008 Author Share Posted February 4, 2008 like this? <?php $arr = print_r($_POST); foreach ($arr as &$value) { mysql_query("insert into paid (user) values ('$value')"); } ?> Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 Yeah that actually made no sense. Had nothing to do with $i. $boxes=$_POST['checkbox']; $i=0; while ($i<=count($boxes)) { mysql_query("insert into paid (user) values ('$boxes[$i]')"); $i++; } I think you can do that. Or else foreach Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 4, 2008 Author Share Posted February 4, 2008 yah it works! Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 4, 2008 Author Share Posted February 4, 2008 now how do I remove the users that I did not select? If I did not select user1, but I selected user2 it inserts a blank for user1 and adds the value for user2? I have this so far: <?php include ("../../style/include/session.php"); include ("../../style/include/cons/head_2.php"); print '<div class="box">'; if($session->isAdmin()){ if(isset($_POST['Submit'])){ $boxes=$_POST['checkbox']; if (empty($boxes)) { //somthing } $i=0; while ($i<=count($boxes)) { mysql_query("insert into paid (user) values ('$boxes[$i]')"); $i++; } echo "<h2>Processed</h2>"; echo "Selected users have been processed"; }else{ echo "<h2>Error!</h2>"; echo 'No data to process<br><a href="paid.php">Select Paid/Non-Paid</a>'; } }else{ header("Location: ../../index.php"); } print '</div>'; include ("../../style/include/cons/foot.php"); ?> Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 $boxes=$_POST['checkbox']; $i=0; while ($i<=count($boxes)) { if($boxes[$i]=='on') { mysql_query("insert into paid (user) values ('$boxes[$i]')"); } $i++; } Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 4, 2008 Author Share Posted February 4, 2008 now it does not insert anything into the DB Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 oh yeah, usually by default the value is 'on' when checked but you set the value. Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 4, 2008 Author Share Posted February 4, 2008 well how do I set the value? like this? <input type="checkbox" name="checkbox[]" value="<?=$rs['username'] ?>" /> so then I need to change this line: if($boxes[$i]=='on') { to this if($boxes[$i]==$rs['username') { ? Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 <input type="checkbox" name="checkbox[]" onclick="if(this.value=='on') { this.value='<?=$rs['username'] ?>'; } if(this.value=='<?=$rs['username'] ?>') { this.value=''; }" /> try changing your input field to that and then the other thing to this: $boxes=$_POST['checkbox']; $i=0; while ($i<=count($boxes)) { if($boxes[$i]!=='') { mysql_query("insert into paid (user) values ('$boxes[$i]')"); } $i++; } Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 4, 2008 Author Share Posted February 4, 2008 now it just inserts a blank row Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 did you put the exclamation point before the equals signs? Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 4, 2008 Author Share Posted February 4, 2008 sure did, I just copied and pasted paid.php <?php include ("../../style/include/session.php"); include ("../../style/include/cons/head_2.php"); print '<div class="box"><h2>Select Paid</h2>'; if($session->isAdmin()){ ?> <form action="pay_do.php" method="post"> <table width="100%" border="0"> <tr> <td width="419"><strong>First Name </strong></td> <td width="487"><strong>Last Name </strong></td> <td width="257"><strong>Paid (username) </strong></td> </tr> <? $qr=mysql_query("select * from users order by first"); while ($rs=mysql_fetch_assoc($qr)) { ?> <tr> <td><? echo $rs['first'];?></td> <td><? echo $rs['last'];?></td> <td><input type="checkbox" name="checkbox[]" onclick="if(this.value=='on') { this.value='<?=$rs['username'] ?>'; } if(this.value=='<?=$rs['username'] ?>') { this.value=''; }" /> (<? echo $rs['username']; ?>)</td> <? } ?> </tr> </table> <br /> <input name="Submit" type="Submit" value="Mark Paid" /> </form> <?php }else{ header("Location: ../../index.php"); } print '</div>'; include ("../../style/include/cons/foot.php"); ?> pay_do.php: <?php include ("../../style/include/session.php"); include ("../../style/include/cons/head_2.php"); print '<div class="box">'; if($session->isAdmin()){ if(isset($_POST['Submit'])){ $boxes=$_POST['checkbox']; $i=0; while ($i<=count($boxes)) { if($boxes[$i]!=='') { mysql_query("insert into paid (user) values ('$boxes[$i]')"); } $i++; } echo "<h2>Processed</h2>"; echo "Selected users have been processed"; }else{ echo "<h2>Error!</h2>"; echo 'No data to process<br><a href="paid.php">Select Paid/Non-Paid</a>'; } }else{ header("Location: ../../index.php"); } print '</div>'; include ("../../style/include/cons/foot.php"); ?> Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 <input type="checkbox" name="checkbox[]" onclick="if(this.checked) { this.value='<?=$rs['username'] ?>'; } else { this.value=''; }" /> Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 Sorry, dumb. <input type="checkbox" name="checkbox[]" onchange="if(this.checked) { this.value='<?=$rs['username'] ?>'; } else { this.value=''; }" /> Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 4, 2008 Author Share Posted February 4, 2008 it still adds a blank row, along with the selected user. That is ok I can live with that. Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 4, 2008 Share Posted February 4, 2008 well, you just have to find out what the value of the checkboxes you dont want is and then set the part of the php that says !=='' to !=='value' and then youre good Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.