Page 1 of 1

How do I add the checkboxes to a database table?

Posted: Thu May 27, 2010 4:19 am
by tom8521
I have this form, but now need to know how to move each box into a database table but don't know how.

<form action="availability.php" method="post">
<input type="checkbox" name="mon9-11" value="mon9-11" />
<input type="checkbox" name="mon11-1" value="mon11-1" />
<input type="checkbox" name="mon1-3" value="mon1-3" />
<input type="checkbox" name="mon3-5" value="mon3-5" />
<input type="checkbox" name="mon5-7" value="mon5-7" />
<input type="checkbox" name="mon7-9" value="mon7-9" />
<input type="submit" name="submit" value="Done" />
<input type="hidden" name="submitted" value="TRUE" />
</form>

Please can someone make the script to move the info to a database table?

Re: How do I add the checkboxes to a database table?

Posted: Thu May 27, 2010 6:51 am
by internet-solution
if chcekboxes are checked by user only then the relevant $_POST / $_GET variable is set, otherwise they are not submitted with the form.

So in availability.php you can check which checkbox is checked by using something like ..

Code: Select all

if(isset($_POST['mon9-11'])) 
$mon911chk=1 
else
$mon911chk==0
Then depending on $mon911chk value you can set your database field. You can automate the process if you use some logic in defining the cehckbox names. Otherwise you will have to write the code for each checkbox.

Re: How do I add the checkboxes to a database table?

Posted: Fri May 28, 2010 9:44 am
by klevis miho
Or you can do:

if(isset($_POST['mon9-11'])) {
//insert this in the database table

Re: How do I add the checkboxes to a database table?

Posted: Fri May 28, 2010 10:06 am
by dimxasnewfrozen
This looks similar:

viewtopic.php?f=1&t=116854

Re: How do I add the checkboxes to a database table?

Posted: Fri May 28, 2010 10:27 am
by phdatabase
Or you could save yourself a lot of typing:
...
<input type='checkbox' name='available[]' value='mon11-1' />
...


to save it:

if( array_key_exists( 'available', $_POST)) {
foreach( $available as $item=>$value) {
${$item) = $value;
}
}

Which will give you a 'list' of values:

...
$mon11-1 = 'mon11-1'
...