Forum Moderators: coopster

Message Too Old, No Replies

Checkbox Script Not working

Checkbox's are not working correctly on my form

         

bzenker

3:26 pm on Jul 3, 2010 (gmt 0)

10+ Year Member



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

rocknbil

4:10 pm on Jul 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Weclome aboard bzenker, checkboxes will only be set in get/post if they are checked which makes parsing them out much easier.

can I extract the unique id value from the database which gets generated automatically and use it as a value for my checkbox?


Yes.

To parse the values, you can use an array,

<input type="checkbox" name="timeslot[]" value="some value">

in which case you'd step through the array,

if (isset($_POST['timeslot'])) {
foreach ($_POST['timeslot'] as $value) {
echo "$value found <br>";
}
}

... or a named field. The named field shouldn't begin with a number (but I guess, will work, it's just a bad idea for various reasons.)

$name = 'timeslot_' . $id;

<input type="checkbox" name="$name" value="some value">

The only difference is you'd have to step through all of $_POST looking for them.

foreach ($_POST as $key=>$value) {
if (preg_match('/timesheet\_\d+/',$key)) {
echo "$key and $value found <br>";
}
}