Getting check box selections to a MySQL database

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
WV Mike
Forum Newbie
Posts: 3
Joined: Fri May 02, 2008 9:25 am

Getting check box selections to a MySQL database

Post by WV Mike »

Greetings,
First post here, so forgive me if this is not a PHP question.

I am working with a friend who has done most of the work on this and I am posting on his behalf as well as mine.

We are trying to create a check box list which will enter the chosen values into a MySQL database.
Currently, the database entries are listed/entered only as "array", not the actual values in the form.

When a radio button list is used then the values will be entered in to the MySQL database.
However, we do not want to use radio buttons, we wish to use a check box form.

Here is the location of the form:
http://epicroadtrips.us/birdlist/form1.html

Additional files used for this are:
http://epicroadtrips.us/birdlist/confirm.html
This generates a page with the results of the checked of entries.
This was the original goal for the this form but it has evolved into something more complicated, and I think, useful.

http://epicroadtrips.us/birdlist/processor.php
As I understand it this is the file which will process the results of the list and send it to the data base.


Any help or ideas appreciated.
Thanks,
-Mike

--
http://www.EpicRoadTrips.us
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Getting check box selections to a MySQL database

Post by pickle »

Your fields are named "field7[]", "field8[]", etc. When the square brackets are in there, each field is treated as an array. The string representation of an array is just "Array" - which is what gets put into the DB. Take off the square brackets.

Also know that checkboxes that are not checked on do not get POSTed.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
WV Mike
Forum Newbie
Posts: 3
Joined: Fri May 02, 2008 9:25 am

Re: Getting check box selections to a MySQL database

Post by WV Mike »

pickle wrote:Your fields are named "field7[]", "field8[]", etc. When the square brackets are in there, each field is treated as an array. The string representation of an array is just "Array" - which is what gets put into the DB. Take off the square brackets.
I will get this info to one working on the code.
pickle wrote: Also know that checkboxes that are not checked on do not get POSTed.
That is what we want - only an entry for each selection which is checked off.

Thanks!
-Mike
http://www.EpicRoadTrips.us
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Getting check box selections to a MySQL database

Post by aceconcepts »

In order to have each checkbox value entered into a database individually you will need to name each checkbox the same:

The form

Code: Select all

 <form name="form1" method="post"><input type="checkbox" name="chkBox[]" /><br /><input type="submit" name="submit" value="submit" /></form> 
The code to add values to a database

Code: Select all

 
if(isset($_POST['submit']))
{
//GET THE CHECKBOX
$chkBox=$_POST['chkBox'];
 
//SIMPLY LOOP OF SELECTED CHECKBOXES
foreach($chkBox as $chk)
{
   //INSERT INTO DATABASE - $chk is the checkbox value
}
}
 
Pretty crude but hopefully gives you some insight.
WV Mike
Forum Newbie
Posts: 3
Joined: Fri May 02, 2008 9:25 am

Re: Getting check box selections to a MySQL database

Post by WV Mike »

aceconcepts wrote:In order to have each checkbox value entered into a database individually you will need to name each checkbox the same:

The form

Code: Select all

 <form name="form1" method="post"><input type="checkbox" name="chkBox[]" /><br /><input type="submit" name="submit" value="submit" /></form> 
The code to add values to a database

Code: Select all

 
if(isset($_POST['submit']))
{
//GET THE CHECKBOX
$chkBox=$_POST['chkBox'];
 
//SIMPLY LOOP OF SELECTED CHECKBOXES
foreach($chkBox as $chk)
{
   //INSERT INTO DATABASE - $chk is the checkbox value
}
}
 
Pretty crude but hopefully gives you some insight.
Thanks. I wll pass this along to the fella who is working the code.
-Mike
Post Reply