How do I add the checkboxes to a database table?

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
tom8521
Forum Commoner
Posts: 25
Joined: Thu May 13, 2010 6:24 am

How do I add the checkboxes to a database table?

Post 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?
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

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

Post 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.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

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

Post by klevis miho »

Or you can do:

if(isset($_POST['mon9-11'])) {
//insert this in the database table
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

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

Post by dimxasnewfrozen »

This looks similar:

viewtopic.php?f=1&t=116854
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

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

Post 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'
...
Post Reply