output from checkboxes

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
andrade1
Forum Newbie
Posts: 14
Joined: Fri Apr 18, 2008 9:16 am

output from checkboxes

Post 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);
 
 
 
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: output from checkboxes

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