bzenker Posted July 3, 2010 Share Posted July 3, 2010 I created a form that has a bunch of checkboxes on it. I am extracting time information from the database and assigning the times to the checkboxes. I want the user to be able to select as many checkboxes as they wish so they can book a timeslot. As they select a timeslot it needs to mark the database field to a 1 to show that timeslot is taken. I am trying to extract the value for the checkbox from the database unique id field because each time entry in the database has a unique id. My understanding is checkboxes are suppose to have unique values. My question is can I extract the unique id value from the database which gets generated automatically and use it as a value for my checkbox? I haven't done a lot with checkboxes in Php besides build basic forms. I am open to any suggestions as I have been trying to get this to work for the last week. This is the first time I used your forum but looks like a great resource. Thanks Link to comment https://forums.phpfreaks.com/topic/206639-checkbox-script-not-working/ Share on other sites More sharing options...
kenrbnsn Posted July 3, 2010 Share Posted July 3, 2010 Please post the code you're having problems with between tags. Ken Link to comment https://forums.phpfreaks.com/topic/206639-checkbox-script-not-working/#findComment-1080733 Share on other sites More sharing options...
bzenker Posted July 3, 2010 Author Share Posted July 3, 2010 //here is the if statement <?php if(sizeof($_POST['box'])) { foreach($_POST['box'] AS $id) { mysql_query("UPDATE chair_appointments SET `allow_participation` = '1' WHERE `resid` = '$id'"); } } ?> //Here is the checkbox part of the form <input type=checkbox name='box[]' value=\"".$row['resid']."\"> //resid is the unique id field in the database for each row of checkboxes. Link to comment https://forums.phpfreaks.com/topic/206639-checkbox-script-not-working/#findComment-1080737 Share on other sites More sharing options...
peter_anderson Posted July 3, 2010 Share Posted July 3, 2010 Are you connected to the database? Not tested, but: <?php if(isset($_POST['box'])){ foreach($_POST['box'] AS $id) { mysql_query("UPDATE chair_appointments SET `allow_participation` = '1' WHERE `resid` = '$id'"); } } ?> Link to comment https://forums.phpfreaks.com/topic/206639-checkbox-script-not-working/#findComment-1080799 Share on other sites More sharing options...
Pikachu2000 Posted July 3, 2010 Share Posted July 3, 2010 Unless I'm missing something, all that loop is doing is overwriting the value of the `allow_participation` field with each iteration. Link to comment https://forums.phpfreaks.com/topic/206639-checkbox-script-not-working/#findComment-1080819 Share on other sites More sharing options...
bzenker Posted July 4, 2010 Author Share Posted July 4, 2010 Thanks to everyone.. I was able to get the code working.. I will be back with more questions in the future. //besides connecting to the database the code below.. a bit messy so will clean up but working as expected. <table width="300" border="0"><tr><td width="30"></td><td><b><u>Start Time</u></b></td><td width="30"></td><td><b><u>End Time</u></b></td></tr></table> <form action="<?=$_SERVER['reserveclientfinal18.php'];?>" method="post"> <table width="500" border="2"> <tr> <td> <?php $query5 = "SELECT chair_appointments.starttime, chair_appointments.endtime, chair_companies.name, chair_appointments.resid from chair_appointments, chair_companies where chair_appointments.scheduleid = chair_companies.scheduleid ORDER BY starttime ASC"; $result2 = mysql_query($query5) or die(mysql_error()); $num2=mysql_numrows($result2); //means if at least one check box is selected $i=0; while ($i < $num2) { $variable1=mysql_result($result2,$i,'starttime'); $variable2=mysql_result($result2,$i,'endtime'); $variable3=mysql_result($result2,$i,'name'); $variable4=mysql_result($result2,$i,'resid'); $i++; $result1=$variable1; $result3=$variable2; $result4=$variable3; $result5=$variable4; if ( $variable2 >= '782') { ?> <table width="350" border="1"> <tr> <td><div align="center"><input type=checkbox name='timeslot[]' value="<?php echo ($result5); ?>"></div></td> <td><input type="hidden" name="starttime[<? echo date('i:s A', $result1-720);?>]" id="starttime" value="<? echo date('i:s A', $result1- 720); ?>"><?php echo date('i:s A', $result1-720); ?></td> <td><input type="hidden" name="endtime[<? echo date('i:s A', $result3-722);?>]" id="endtime" value="<? echo date('i:s A', $result3-722); ?>"><?php echo date('i:s A', $result3-722); ?></td> <td><input type="hidden" name="name[<? echo ($result4);?>]" id="name" value="<? echo ($result4); ?>"><?php echo ($result4); ?></td> </tr> </table> <?php } } ?> </td> </tr></table><table><tr><td colspan="3"><input type="submit" value="Update" /></td></tr></table> </form> <?php $_POST['timeslot']; if($_POST['timeslot'] != NULL){ foreach($_POST['timeslot'] as $k => $c){ //<-- prints the EXACT correct information for the sql query to be properly executed. $sql = "UPDATE chair_appointments SET allow_participation=1 WHERE resid = '". $c ."'"; mysql_query($sql) OR die ("The query:<br>" . $sql . "<br>Caused the following error:<br>" . mysql_error()); $i++; } } ?> Link to comment https://forums.phpfreaks.com/topic/206639-checkbox-script-not-working/#findComment-1081043 Share on other sites More sharing options...
Genesis730 Posted July 5, 2010 Share Posted July 5, 2010 Please click the green button located on the bottom left which says "MARK SOLVED" when you have your answer Thank you Link to comment https://forums.phpfreaks.com/topic/206639-checkbox-script-not-working/#findComment-1081172 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.