Page 1 of 1

output from checkboxes

Posted: Fri May 30, 2008 9:02 am
by andrade1
Hello

I have 12 checkboxes that I am trying to send to a mysql database. How can I get more than one of them to display in the database? Currently if more than one checkbox is checked only one of the boxes is being displayed in the database and I have the type in sql setup for text.

Code: Select all

 
 
if (isset($_POST['DEANAHSS2']))
$RSP = 'DEANAHSS2';
 
 
if (isset($_POST['PRES2']))
$RSP = 'PRES2';
 
if (isset($_POST['VPIT2']))
$RSP = 'VPIT2';
 
 
if (isset($_POST['DEANCNMS2']))
$RSP = 'DEANCNMS2';
 
 
if (isset($_POST['PROV2']))
$RSP = 'PROV2';
 
 
if (isset($_POST['VPRDGS2']))
$RSP = 'VPRDGS2';
 
 
if (isset($_POST['DEANCOEIT2']))
$RSP = 'DEANCOEIT2';
 
 
if (isset($_POST['VPAF2']))
$RSP = 'VPAF2';
 
 
if (isset($_POST['DEANERK']))
$RSP = 'DEANERK';
 
 
if (isset($_POST['VPDUE']))
$RSP = 'VPDUE';
 
 
if (isset($_POST['DEANUE2']))
$RSP = 'DEANUE2';
 
 
if (isset($_POST['VPIA2']))
$RSP = 'VPIA2';
 
$query = "INSERT INTO Log (DateRcvd, OrigDept, OrigContact, RespOffice, contents,  refer, request, deadline, GivenTo, stats, outcome, notes, complete) 
VALUES ('$drcvd', '$ODT', '$ocont', '$RSP', '$contents', '$REF', '$REQ', '$deadline', '$givento', '$status', '$outcome', '$notes', '$completed')";
 
$result = mysql_query($query);
 
 
 

Re: output from checkboxes

Posted: Fri May 30, 2008 9:47 am
by nowaydown1
The problem you're having is two fold. First, you're overwriting the $RSP variable every time if more than one thing is checked, so you'll always get the last value set by your conditionals. Secondly, that value is being pumped into a single column.

First, the single checkbox issue. To allow multiple checkboxes to be passed you should force the name to be an array, like:

Code: Select all

 
<input type="checkbox" name="respOffice[]" value="DEANAHSS2"  /> DEANAHSS2
<input type="checkbox" name="respOffice[]" value="PRES2"  /> PRES2
 
Then you can just loop over whatever is in $_POST["respOffice"]. Then, to get the data in the database you could implode it and store it as a CSV string, or make the appropriate schema changes.