PDA

View Full Version : how to get value of each checked check-box



starfish
10-22-2008, 02:15 AM
hi...
the problem i am facing is that
i want to insert a value for each check box if it is checked by the user...
doctor has to select the days that would be working for him/her using checkboxes
if he checks the checkbox for monday
an entry in "working_days" table would be inserted for this doctor
one method i applied and got success is to check the value using isset()
a part from code is given below

if(isset($_REQUEST['mon']))
{
if($this->chk_dup_workingday($doc_id,$_REQUEST['mon'])==0)
{
$ins="insert into working_day (doctor_id, day_id) values('$doc_id', '$_REQUEST[mon]')";
$mark=mysql_query($ins) or die("Couldn't Insert Working day : ".mysql_error());
if($mark)
{
?> <script>
alert("Inserted Monday as a Working Day!");
</script>
<?
}
}
}

the problem is to optimize the code i mean it is a lengthy code / method
is there any other method / concept .........to achieve this.....
since i have to put this code for each check box to make an entry for the checked one....

kajnes
10-22-2008, 06:45 PM
not sure if this will help but I had problem with inserting into db 'x' number of photos with 'x' numer of options (from checkboxes)...

if (is_array($_POST['photo_id'])) {
foreach ($_POST['photo_id'] as $photoIndex=>$photoId) {

// clear all option for photos
$fotka="$photoId";
mysql_query("DELETE FROM table WHERE foto_id='$fotka' ");

if (is_array($_POST['checkbox_'.$photoId])) {
foreach ($_POST['checkbox_'.$photoId] as $optionIndex=>$optionId) {
// debug echo("Wybrana opcja id=".$optionId."<br>");
$ido="$optionId";

// insert new options
mysql_query("INSERT INTO table (foto_id, opcja) VALUES('$fotka','$ido')") or die(mysql_error());

}
}
}
}



and forms;
"<input type=\"hidden\" name=\"photo_id[]\" value=\"$row22[0]\">");
"<input type=\"checkbox\" name=\"checkbox_$row22[0][]\" value=\"$row[1]\"";