I think I get what your after. Do you want to stop for submission if there is a problem with the check boxes? ( none selected ) and use java to display errors before the form is submited or are you going to deal with error handeling after the form is submited?
I ask because the solution depends on how the data is tranmited to the server from the form for processing.
If you want Javascript to collect and verify the data before the form is sent then you need to somehow collect all the form eliments and then send them using Javascript instead of html submission. The Check box array should be sent as a JSON encoded string and decoded by PHP on the server. then you can explode the values for the check boxes. This is complex, but allows for better error handeling
if you are going to use HTML to submit the form and PHP to deal with errors then you can A) use the built in checkbox array and then use PHP to step through that array pulling the needed values and building your sql string from them. This way you have tyo have an array of keys to match up with the check box array once its at the server os that the correct values can be match witht eh correct keys. or
you can give each check box a unique name/ID and simply pull them from the $_REQUEST array and put them in the query string.
In the code I posted I only wanted a JSON string to stor in the DB and used javascript to decode it when I was needed again. This made it easy to do, but made extending functionality and reusing that data else where much harder. in the ends I converted the JSON string to a comma deliminated string. that was then exploded and the vaules passed to an array with the needed keys. Then I steped through that array and build the SQL query.
The final version was something like below. ( this is stripped down )
As this form was NEVER designed to be submited, but rather to update the database 'live', I had to go with the onchange command to trigger the javascript. This way you could take the $formArray ['requests'] as your list of ALL room numbers and the $requestList as your list of selected rooms already stored in the DB.
$requestList = $reportObj->getRequestList ();
$i = 0;
$checkedArray=Array();
if (isset ( $formArray ['requests'] ) && ! empty ( $formArray ['requests'] )) {
$checkedArray = explode(',', $formArray ['requests']);
}
foreach ( $requestList as $key => $request ) {
?><li><?php
if (isset ( $checkedArray ) && in_array ( $key, $checkedArray )) {
?><label><input TABINDEX='<?=$tabIndex++?>' class='comLog' type='checkbox' name='requests_cb' id='requests_cb' value='<?=$key?>' onchange="putData('ajax/accidentReport.ajax.php', '<?=$reportID?>', 'requests', '', this.id);" checked> <?=$request?></label><?php
}else{
?><label><input TABINDEX='<?=$tabIndex++?>' class='comLog' type='checkbox' name='requests_cb' id='requests_cb' value='<?=$key?>' onchange="putData('ajax/accidentReport.ajax.php', '<?=$reportID?>', 'requests', '', this.id);"> <?=$request?></label><?php
}
?>
</li>
<?php
$i ++;
}
The accidentReport.ajax file
if (isset ( $_REQUEST ['reportID'] )) {
$msg = "Report ID = {$_REQUEST['reportID']}";
config::writeLog($msg);
$reportID = $_REQUEST ['reportID'];
} else {
$msg = "error no reportID Found";
config::writeLog($msg);
exit ();
}
// get value from post array
if (isset ( $_REQUEST ['value'] )) {
$val = $_REQUEST ['value'];
} else {
$msg = "error no value found";
config::writeLog($msg);
exit ();
}
// get the column to update
if (isset ( $_REQUEST ['col'] )) {
$col = $_REQUEST ['col'];
// fix for ems call times
switch ($col) {
case 'emsCallTime' :
case 'emsArrivalTime' :
case 'emsDepartTime' :
if ($val == false) {
$val = "";
} else {
$val = strtotime ( $val );
}
break;
}
} else {
$msg = "error no colum name found";
config::writeLog($msg);
exit ();
}
// update the report where col to = val
$query = "UPDATE " . config::table_prefix . "accident_log SET $col = '".mysql_escape_string($val)."' WHERE reportID = '$reportID' LIMIT 1";
if($sqlObj->query($query)){
echo $val;
}else{
$msg = "Error updating. $query";
config::writeLog($msg);
}
As you can see the $val is returned to the JS putData method using the echo command.
I hope this helps, Its hard to apply these things to other peoples projects, but I think what you are after canbe achived using a method simler to this. Using these methods ( edited of course ) you should be easaly be able to create a live Ajax application that does not requier any real form submission and still the data updated on the DB.
for your table I would say you need three colums id(int with auto incremented value), seatNumber (varchar(unique)) and booked(boolean or bit value)
so your update stament would look somthing like this. ( guessing at the names you use)
$query = "UPDATE seatBookings SET booked = '".$booked.'" WHERE seatNumber = '".$seatNumber."' LIMIT 1";
Finaly please note I wrote this code a couple of years ago and I would not approch this problem in the same manner now as I did then. It works yes, but I was never happy with the code. The result was as desiered though, and in the end thats what matters to the client.