Forums

This topic is locked

List box and Check box

Posted 12 Apr 2005 00:16:20
1
has voted
12 Apr 2005 00:16:20 Dan Bell posted:
This is my second post regarding using Dreamweaver to create multiple selections using PHP, Mysql and Dreamweaver.

Background: I am working a faculty member at Webster University to develop materials for teaching Advance Web design. He want to teach them how to create quizzes that can be administered online with the results going into a database. In the past they wrote thier responses to a file or an email message without experiencing problems. As soon as we create a quizz that stored the info in MySQL we experienced problems with saving multiple check boxes to the same field as well as multiple selections from a list box.

I have looked at the set and enum data types and think that It might be the problem but I am not sure how to correct it. I am able to get one item but not multiple selections into the set datatype.

My question is how do you create a page using Dreamweaver and its built in PHP-Mysql tools that will save the response to a question using checkboxes to record the answers when there are more than one item chosen.

Replies

Replied 12 Apr 2005 00:31:08
12 Apr 2005 00:31:08 Chris Charlton replied:
Post your SQL code that does the INSERT or UPDATE.

~ ~ ~ ~ ~ ~ ~
Chris Charlton <i>- DMXzone Manager</i>
<font size=1>[ Studio MX/MX2004 | PHP/ASP | SQL | XHTML/CSS | XML | Actionscript | Web Accessibility | MX Extensibility ]</font id=size1>
Replied 12 Apr 2005 18:00:48
12 Apr 2005 18:00:48 Dan Bell replied:
I have had some success since yesterday. For Questions 2 and 3 I am able to get the database to accept the first two selections using the set datatype and inserting integer values for each option. I cannot though get it to work with more that 2 selections.

thank you
HERE IS THE CODE FOR INSERTING A NEW RECORD:

&lt;?php require_once('../Connections/hermes.php'); ?&gt;
&lt;?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = ""
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "" ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "" ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "" ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "" ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "" ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") {
$insertSQL = sprintf("INSERT INTO users_usr (question_01, question_02, question_03) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['Q1RadioGroup'], "text",
GetSQLValueString($_POST['Q2checkbox'], "int",
GetSQLValueString($_POST['select'], "int");

mysql_select_db($database_hermes, $hermes);
$Result1 = mysql_query($insertSQL, $hermes) or die(mysql_error());

$insertGoTo = "sampletest.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?&gt;
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;title&gt;Sample Test&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;p&gt;&lt;strong&gt;Sample Test
&lt;/strong&gt;&lt;/p&gt;
&lt;form name="form1" method="POST" action="&lt;?php echo $editFormAction; ?&gt;"&gt;
&lt;p&gt;Q1 Cytology is the study of Cells? &lt;/p&gt;
&lt;table width="200"&gt;
&lt;tr&gt;
&lt;td&gt;&lt;label&gt;
&lt;input type="radio" name="Q1RadioGroup" value="true"&gt;
True&lt;/label&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;label&gt;
&lt;input type="radio" name="Q1RadioGroup" value="false"&gt;
False&lt;/label&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;Q2 What are the Shapes of a cell? &lt;br&gt;
(Check all that apply) &lt;/p&gt;
&lt;p&gt;
&lt;input name="Q2checkbox" type="checkbox" id="Q2checkbox" value="1"&gt;
spiral,
&lt;input name="Q2checkbox" type="checkbox" id="Q2checkbox" value="2"&gt;
cocci,
&lt;input name="Q2checkbox" type="checkbox" id="Q2checkbox" value="3"&gt;
bacillus,
&lt;input name="Q2checkbox" type="checkbox" id="Q2checkbox" value="4"&gt;
unicellular&lt;/p&gt;
&lt;p&gt;Q3Which organelle controls the cell? &lt;br&gt;
(select one or more)
&lt;/p&gt;
&lt;p&gt;
&lt;select name="select" size="4" multiple&gt;
&lt;option value="1"&gt;mitochondria&lt;/option&gt;
&lt;option value="2"&gt;cell wall&lt;/option&gt;
&lt;option value="3"&gt;nucleus&lt;/option&gt;
&lt;option value="4"&gt;vacuole&lt;/option&gt;
&lt;/select&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;input name="Submit " type="submit" id="Submit " value="Submit Test"&gt;
&lt;/p&gt;
&lt;input type="hidden" name="MM_insert" value="form1"&gt;
&lt;/form&gt;
&lt;p&gt;&lt;strong&gt; &lt;/strong&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
Replied 13 Apr 2005 15:14:59
13 Apr 2005 15:14:59 Matt Bailey replied:
A friend helped me edit DW's standard Insert code so it could input mulitple database rows at one time by looping over the checkboxes using an array. Here are the relevant bits of code, maybe it will be of some use to you:


HTML:

<pre id=code><font face=courier size=2 id=code>&lt;table width="380" border="0" cellspacing="0" cellpadding="10"&gt;
&lt;tr&gt;
&lt;th width="280"&gt;Item Name&lt;/th&gt;
&lt;th width="100"&gt;Checkbox&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="280"&gt;Item 1
&lt;input name="ItemName[1]" type="hidden" id="ItemName[1]" value="Item 1"&gt;&lt;/td&gt;
&lt;td width="100"&gt;&lt;input type="checkbox" name="ItemID[1]" value="1"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="280"&gt;Item 2
&lt;input name="ItemName[2]" type="hidden" id="ItemName[2]" value="Item 2"&gt;&lt;/td&gt;
&lt;td width="100"&gt;&lt;input type="checkbox" name="ItemID[2]" value="2"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="280"&gt;Item 3
&lt;input name="ItemName[3]" type="hidden" id="ItemName[3]" value="Item 3"&gt;&lt;/td&gt;
&lt;td width="100"&gt;&lt;input type="checkbox" name="ItemID[3]" value="3"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="280"&gt;Item 4
&lt;input name="ItemName[4]" type="hidden" id="ItemName[4]" value="Item 4"&gt;&lt;/td&gt;
&lt;td width="100"&gt;&lt;input type="checkbox" name="ItemID[4]" value="4"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;</font id=code></pre id=code>


PHP:

<pre id=code><font face=courier size=2 id=code>if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "MultipleInsertForm") {
//only do the insert if we have any data
if(is_array($HTTP_POST_VARS['ItemID'])){
//loop over checked boxes
foreach ($HTTP_POST_VARS['ItemID'] as $MultipleInsert_key =&gt; $MultipleInsert_item){
$insertSQL = sprintf("INSERT INTO MultipleInsert_Selections (ItemID, ItemName) VALUES (%s, %s)",
GetSQLValueString($MultipleInsert_item, "int",
GetSQLValueString($HTTP_POST_VARS['ItemName'][$MultipleInsert_key], "text");

mysql_select_db($database_MultipleInsertConn, $MultipleInsertConn);
$Result1 = mysql_query($insertSQL, $MultipleInsertConn) or die(mysql_error());
}}</font id=code></pre id=code>

___________________________________
* Sorry... how do you do that again?... *
Replied 13 Apr 2005 17:24:55
13 Apr 2005 17:24:55 Dan Bell replied:
Thank you very much. I will give it a try.

Dan

Reply to this topic