Page 1 of 1

Collecting Multiple Answers for One Question with Checkboxes

Posted: Mon Jul 01, 2002 10:36 pm
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

Change the "name" attributes

Posted: Mon Jul 01, 2002 10:54 pm
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.

Posted: Mon Jul 01, 2002 11:26 pm
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

Posted: Tue Jul 02, 2002 1:03 am
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.

Almost there I hope.

Posted: Wed Jul 03, 2002 3:58 am
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

Posted: Wed Jul 03, 2002 4:39 am
by twigletmac
Maybe if you show us your code we can point out where you might be going wrong.

Mac

Here's the code..

Posted: Wed Jul 03, 2002 5:17 am
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;

?>

Posted: Wed Jul 03, 2002 5:30 am
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

Posted: Wed Jul 03, 2002 6:05 am
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()

Posted: Wed Jul 03, 2002 6:12 am
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