Forums
This topic is locked
List box and Check box
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>
~ ~ ~ ~ ~ ~ ~
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:
<?php require_once('../Connections/hermes.php'); ?>
<?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));
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Sample Test</title>
</head>
<body>
<p><strong>Sample Test
</strong></p>
<form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<p>Q1 Cytology is the study of Cells? </p>
<table width="200">
<tr>
<td><label>
<input type="radio" name="Q1RadioGroup" value="true">
True</label></td>
</tr>
<tr>
<td><label>
<input type="radio" name="Q1RadioGroup" value="false">
False</label></td>
</tr>
</table>
<p>Q2 What are the Shapes of a cell? <br>
(Check all that apply) </p>
<p>
<input name="Q2checkbox" type="checkbox" id="Q2checkbox" value="1">
spiral,
<input name="Q2checkbox" type="checkbox" id="Q2checkbox" value="2">
cocci,
<input name="Q2checkbox" type="checkbox" id="Q2checkbox" value="3">
bacillus,
<input name="Q2checkbox" type="checkbox" id="Q2checkbox" value="4">
unicellular</p>
<p>Q3Which organelle controls the cell? <br>
(select one or more)
</p>
<p>
<select name="select" size="4" multiple>
<option value="1">mitochondria</option>
<option value="2">cell wall</option>
<option value="3">nucleus</option>
<option value="4">vacuole</option>
</select>
</p>
<p>
<input name="Submit " type="submit" id="Submit " value="Submit Test">
</p>
<input type="hidden" name="MM_insert" value="form1">
</form>
<p><strong> </strong></p>
</body>
</html>
thank you
HERE IS THE CODE FOR INSERTING A NEW RECORD:
<?php require_once('../Connections/hermes.php'); ?>
<?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));
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Sample Test</title>
</head>
<body>
<p><strong>Sample Test
</strong></p>
<form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<p>Q1 Cytology is the study of Cells? </p>
<table width="200">
<tr>
<td><label>
<input type="radio" name="Q1RadioGroup" value="true">
True</label></td>
</tr>
<tr>
<td><label>
<input type="radio" name="Q1RadioGroup" value="false">
False</label></td>
</tr>
</table>
<p>Q2 What are the Shapes of a cell? <br>
(Check all that apply) </p>
<p>
<input name="Q2checkbox" type="checkbox" id="Q2checkbox" value="1">
spiral,
<input name="Q2checkbox" type="checkbox" id="Q2checkbox" value="2">
cocci,
<input name="Q2checkbox" type="checkbox" id="Q2checkbox" value="3">
bacillus,
<input name="Q2checkbox" type="checkbox" id="Q2checkbox" value="4">
unicellular</p>
<p>Q3Which organelle controls the cell? <br>
(select one or more)
</p>
<p>
<select name="select" size="4" multiple>
<option value="1">mitochondria</option>
<option value="2">cell wall</option>
<option value="3">nucleus</option>
<option value="4">vacuole</option>
</select>
</p>
<p>
<input name="Submit " type="submit" id="Submit " value="Submit Test">
</p>
<input type="hidden" name="MM_insert" value="form1">
</form>
<p><strong> </strong></p>
</body>
</html>
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><table width="380" border="0" cellspacing="0" cellpadding="10">
<tr>
<th width="280">Item Name</th>
<th width="100">Checkbox</th>
</tr>
<tr>
<td width="280">Item 1
<input name="ItemName[1]" type="hidden" id="ItemName[1]" value="Item 1"></td>
<td width="100"><input type="checkbox" name="ItemID[1]" value="1">
</td>
</tr>
<tr>
<td width="280">Item 2
<input name="ItemName[2]" type="hidden" id="ItemName[2]" value="Item 2"></td>
<td width="100"><input type="checkbox" name="ItemID[2]" value="2">
</td>
</tr>
<tr>
<td width="280">Item 3
<input name="ItemName[3]" type="hidden" id="ItemName[3]" value="Item 3"></td>
<td width="100"><input type="checkbox" name="ItemID[3]" value="3">
</td>
</tr>
<tr>
<td width="280">Item 4
<input name="ItemName[4]" type="hidden" id="ItemName[4]" value="Item 4"></td>
<td width="100"><input type="checkbox" name="ItemID[4]" value="4">
</td>
</tr>
</table></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 => $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?... *
HTML:
<pre id=code><font face=courier size=2 id=code><table width="380" border="0" cellspacing="0" cellpadding="10">
<tr>
<th width="280">Item Name</th>
<th width="100">Checkbox</th>
</tr>
<tr>
<td width="280">Item 1
<input name="ItemName[1]" type="hidden" id="ItemName[1]" value="Item 1"></td>
<td width="100"><input type="checkbox" name="ItemID[1]" value="1">
</td>
</tr>
<tr>
<td width="280">Item 2
<input name="ItemName[2]" type="hidden" id="ItemName[2]" value="Item 2"></td>
<td width="100"><input type="checkbox" name="ItemID[2]" value="2">
</td>
</tr>
<tr>
<td width="280">Item 3
<input name="ItemName[3]" type="hidden" id="ItemName[3]" value="Item 3"></td>
<td width="100"><input type="checkbox" name="ItemID[3]" value="3">
</td>
</tr>
<tr>
<td width="280">Item 4
<input name="ItemName[4]" type="hidden" id="ItemName[4]" value="Item 4"></td>
<td width="100"><input type="checkbox" name="ItemID[4]" value="4">
</td>
</tr>
</table></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 => $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
Dan