Checkbox Script Not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bzenker
Forum Newbie
Posts: 2
Joined: Sat Jul 03, 2010 10:40 am

Checkbox Script Not working

Post by bzenker »

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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Checkbox Script Not working

Post by califdon »

Check out this very short tutorial: http://www.htmlcodetutorial.com/forms/_ ... CKBOX.html
bzenker
Forum Newbie
Posts: 2
Joined: Sat Jul 03, 2010 10:40 am

Re: Checkbox Script Not working

Post by bzenker »

I was able to get the code working.. Thanks again for the help. Here is the code except connecting to the database so if it helps anyone out great.. I have to clean up the code but at least it is running..

Code: Select all

<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['reserveclientfinal19.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++;
}
}
?>
Post Reply