Forum Moderators: coopster

Message Too Old, No Replies

form checkbox results to MySQL database

         

djkaytee

3:11 pm on Jan 26, 2008 (gmt 0)

10+ Year Member



Hi everyone,

I'm struggling with getting checkbox results from a form into a MySQL database.

I think the following things are issues:
- what values to allow in the relevant fields in the database
- what values to give the checkboxes
- do I need an array for my checkboxes
- how to handle checkboxes that aren't checked - seems to give 'undefined index' error

This is what I have so far
- in my form, my set of checkboxes:

<input type="checkbox" name="GardenersWorld" value="GardenersWorld">Gardeners World</input><br>

<input type="checkbox" name="OrganicGardening" value="OrganicGardening">Organic Gardening</input><br>

<input type="checkbox" name="GrowYourOwn" value="GrowYourOwn">Grow Your Own</input><br>

- in my database the relevant fields are:
GardenersWorld enum('Y', 'N')
OrganicGardening enum('Y', 'N')
GrowYourOwn enum('Y', 'N')

So how do I get the results of checked checkboxes into my database so that checked => 'Y' and unchecked => 'N'? Or happy to change accepted database inputs.

Would really appreciate any tips.
Thank you. Katie

cameraman

5:45 pm on Jan 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With PHP, if a checkbox is not checked then the $_POST array won't have that element at all. So for your purposes, just check the existence of each in turn using isset().
if(isset($_POST['GardenersWorld']))
$gw = 'Y';
else
$gw = 'N';

or a little more concise
$gw = isset($_POST['GardenersWorld'])? 'Y' : 'N';

Do that for each checkbox and build your sql with the resulting variables you create.

djkaytee

6:02 pm on Jan 26, 2008 (gmt 0)

10+ Year Member



Thanks cameraman, that's clear and simple, I'll give it a go.
Appreciate your help.
Katie