Collecting Multiple Answers for One Question with Checkboxes

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
stevesoler
Forum Newbie
Posts: 7
Joined: Sun Jun 30, 2002 4:11 am

Collecting Multiple Answers for One Question with Checkboxes

Post by stevesoler »

Collecting Multiple Answers for One Question with HTML Checkboxes, PHP & MySQL

Hello everyone! This is regarding PHP & MySQL.

I have a form that website users fill out and send me there comments. Using a PHP script I created, the comments are posted to a MySQL table and I retrive them via a private admin section.

My Questions:

How do I use HTML Form Checkboxes to collect multiple answers for one question? What Column Data String Type should I use to store the answers? I tried using VARCHAR but it only stores one checkbox even when the user selects more.

Here is the html I use for the form question.
All the checkboxes are named the same, which is I belive how it should be:

Preferred Response Methods:

Code: Select all

<input type="checkbox" name="Respond_Via" value="E-mail" border="0"> E-mail.
<input type="checkbox" name="Respond_Via" value="Phone" border="0"> Phone.
<input type="checkbox" name="Respond_Via" value="Fax" border="0"> Fax. 
<input type="checkbox" name="Respond_Via" value="Mail" border="0"> Mail.  
<input type="checkbox" name="Respond_Via" value="None" border="0"> None.
Many thanks to anyone who can help me with this!
- Steve 8)

Just incase you need to know.. my current setup is:
Solaris 2.7 release 05/99
Apache 1.3.3
MySQL Version 11.15 Distribution 3.23.43
PHP 4.1.0 Apache Modules
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Change the "name" attributes

Post by llimllib »

Variables are passed to the action page based on their "name" attribute in the <input> tag. If you change it to read:

Code: Select all

<input type="checkbox" name="email" value="E-mail" border="0"> E-mail.
<input type="checkbox" name="phone" value="Phone" border="0"> Phone.
<input type="checkbox" name="fax" value="Fax" border="0"> Fax.
<input type="checkbox" name="mail" value="Mail" border="0"> Mail. 
<input type="checkbox" name="none" value="None" border="0"> None.
the result of each checkbox should be available in the $_POST[] array (or the $_GET[] array if method="get"). For example, the value of the email checkbox will be $_POST['email'], and the phone box will be $_POST['phone']. The type of the mysql column is unimportant here.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Or I believe you can do
<input type="checkbox" name="Respond_Via[]" value="E-mail" border="0"> E-mail.
<input type="checkbox" name="Respond_Via[]" value="Phone" border="0"> Phone.
<input type="checkbox" name="Respond_Via[]" value="Fax" border="0"> Fax.
<input type="checkbox" name="Respond_Via[]" value="Mail" border="0"> Mail.
<input type="checkbox" name="Respond_Via[]" value="None" border="0"> None.
and they will arrive at the destination as an array, which is easier to deal with
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Yes, using an array as the name is generally how this is done. It enables all the options to be encapsulated in one array variable and makes it much simpler to work with.
stevesoler
Forum Newbie
Posts: 7
Joined: Sun Jun 30, 2002 4:11 am

Almost there I hope.

Post by stevesoler »

Hi everyone. Thanks for the info.

I'm going to use an array. I've renamed my checkboxes as shown below:

Code: Select all

<input type="checkbox" name="Respond_Via&#1111;]" value="E-mail" border="0"> E-mail. 
<input type="checkbox" name="Respond_Via&#1111;]" value="Phone" border="0"> Phone. 
<input type="checkbox" name="Respond_Via&#1111;]" value="Fax" border="0"> Fax. 
<input type="checkbox" name="Respond_Via&#1111;]" value="Mail" border="0"> Mail. 
<input type="checkbox" name="Respond_Via&#1111;]" value="None" border="0"> None.
Now how do I insert this array into my table? I tried it out, but it still inserts only the last checkbox that was selected in the array.

Thanks,
Steve
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Maybe if you show us your code we can point out where you might be going wrong.

Mac
stevesoler
Forum Newbie
Posts: 7
Joined: Sun Jun 30, 2002 4:11 am

Here's the code..

Post by stevesoler »

twigletmac wrote:Maybe if you show us your code we can point out where you might be going wrong.
Good Idea twigletmac. Thanks.

Code: Select all

<?php

include("/public/include/dbconnection.php");

if ($First_Name !="" && $Last_Name !="" && $Message_Category !="" && $Subject !="" && $Message !="" && $State !="" && $Country !="" ) &#123;

$sql = "INSERT INTO $table_name
(Date,First_Name,Last_Name,Title,Company,Website,Message_Category,Subject, Message,Respond_Via,Email,Work_Phone,Home_Phone,Other_Phone,Fax, Mailing_Address,City,State,Province,Zip_Postal_Code,Country,Reference,Contact_List_Opt_in)
VALUES
(now(),"$First_Name","$Last_Name","$Title","$Company","$Website", "$Message_Category","$Subject","$Message","$Respond_Via","$Email", "$Work_Phone","$Home_Phone","$Other_Phone","$Fax","$Mailing_Address","$City","$State","$Province","$Zip_Postal_Code","$Country", "$Reference","$Contact_List_Opt_in")";
$result = @mysql_query($sql, $connection) or die("Error #". mysql_errno() . ": " . mysql_error());

include("WebForm_added.html");

&#125; else &#123;

include("WebForm_missing.html");

&#125;

?>
Last edited by stevesoler on Wed Jul 03, 2002 5:45 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

(if you edit your code above and put spaces after the commas in the SQL statement it will allow it to line wrap and so the page won't get pushed out to the side)

Anyway, you can use implode() to put all the values into a string before you put it in the database, so:

Code: Select all

$Respond_Via = implode('|', $Respond_Via);
If you have two or more bits of data in the array the string will look something like this: Email|Phone|Mail.

To use the information again when you take it out of the database you would then use explode():

Code: Select all

$Respond_Via = explode('|', $info_from_db);
This will create an array of the values.

Mac
stevesoler
Forum Newbie
Posts: 7
Joined: Sun Jun 30, 2002 4:11 am

Post by stevesoler »

Where exactly do I place it in my code? I tried in a couple of spots and I get this error:

Warning: Bad arguments to implode()
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

After the form has been posted then the values of the array should be available (if any of the check boxes have been selected), maybe something like:

Code: Select all

<?php 

include("/public/include/dbconnection.php"); 

if (!empty($First_Name) && !empty($Last_Name) && !empty($Message_Category) && !empty($Subject) && !empty($Message) && !empty($State) && !empty($Country)) &#123; 

if (isset($Respond_Via) && is_array($Respond_Via)) &#123;  // if the variable has been set and is an array.
    $Respond_Via = implode('|', $Respond_Via);
&#125; else &#123; // If no check box was selected.
    $Respond_Via = 'none';
&#125;
// Rest of code as before.
Mac
Post Reply