Page 1 of 1

Getting check box selections to a MySQL database

Posted: Fri May 02, 2008 9:28 am
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

Re: Getting check box selections to a MySQL database

Posted: Fri May 02, 2008 9:45 am
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.

Re: Getting check box selections to a MySQL database

Posted: Fri May 02, 2008 10:50 am
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

Re: Getting check box selections to a MySQL database

Posted: Fri May 02, 2008 11:27 am
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.

Re: Getting check box selections to a MySQL database

Posted: Fri May 02, 2008 11:54 am
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