Trouble dynamically creating a list of checkboxes
| From: | Amanda McComb | Date: | Tue, 22 Apr 2003 15:47:04 +0000 |
| Subject: | Trouble dynamically creating a list of checkboxes | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-144849@lists.php.net to get a copy of this message | ||
I am trying to create a form that is dynamically built by getting the info
from a database. There are a couple of things I'm stuck on.
First, there is one part where the customer selects a conference from a
dynamically created drop down list. Depending on what conference they
choose, a list of conference components is generated. Each component has
a text box associated with it for them to enter the quantity of components
they want. I'm not sure what to name the quantity boxes to differentiate
them from one another, and also how to get the data out after the form is
submitted.
Second, a list of checkboxes is dynamically created for the customer to
pick and choose some items. I'm not sure how to name these, either, or
how to get the data out of the variables after the form is submitted
(either to inssert it into the database, or re-display it if the person
did not answer all of the required fields).
I am really hoping someone can help with this. I know I've done this
before in the past, but I've just recently started using php again after a
loooong break, so I forget how I did these things before!
I've included some of the code below. I've stripped out tons of
irrelevant stuff, added comments (usually starting with ????) around the
places I'm having trouble. The code probably won't work, because I've
been playing with it to get it to work for several hours. There's also a
lot of it, and I apologize.
<?
if (($REQUEST_METHOD=='POST')) {
for(reset($HTTP_POST_VARS);
$key=key($HTTP_POST_VARS);
next($HTTP_POST_VARS)) {
$this = addslashes($HTTP_POST_VARS[$key]);
$this = strtr($this, ">", " ");
$this = strtr($this, "<", " ");
$this = strtr($this, "|", " ");
$$key = $this;
}
//Make a list of required fields and check for them
//if ($add && ) {
//What do I do here? How do I get those form values to insert?
//$query = "INSERT INTO customer_t ";
//rest of query
//mysql_query($query) or die("Insert Failed!");
//} else {
// $notall = 1;
//}
if ($notall == 1) { ?>
<P><FONT COLOR="red">Please answer all fields</FONT></P>
<? }else{ ?>
<P><FONT COLOR="red"> '<? echo stripslashes($first_name);?>
<?
echo stripslashes($last_name);?>' has been saved.</FONT></P>
<? }
} ?>
<form action=<? echo $PHP_SELF?> method="POST" name="listSelect"
enctype="multipart/form-data">
<TABLE WIDTH='90%' BORDER=0>
<TR>
<TD COLSPAN=4>
<TABLE WIDTH='100%'>
<tr bgcolor="brown"><TD align=center><font
color='white'><B>Conference</B></font></TD></TR>
</TABLE>
</TD>
</TR>
<TR>
<TD>
Conference:
</TD>
<TD>
<?//build a drop down of all available conferences
$query = "SELECT * FROM conference_t WHERE retired_flag is NULL
OR
retired_flag = 0 ORDER BY conference_name";
$search_results = mysql_query($query) or die("Select Failed!");?>
<script language="JavaScript">
function selectList() {
if (this.selectedIndex = 0)
alert("Please select a valid Conference from the List.");
else {
document.listSelect.submit();
}
}
</script>
<select name="conference_cd"
onChange="javascript:selectList();">
<option value="">
<? while ($search_result = mysql_fetch_array($search_results)) {?>
<? //The price is in another table - select it for each conference
$subquery = "SELECT conference_component_price FROM
conference_component_t WHERE conference_cd
= $search_result[conference_cd] and component_cd = 1 and (retired_flag
is NULL OR retired_flag = 0)";
$results = mysql_query($subquery) or die("Select Failed!");
$result = mysql_fetch_array($results);
?>
<option value="<? echo $search_result['conference_cd']?>"
<? if ($conference_cd){
if ($conference_cd == $search_result['conference_cd']){
$selected = "SELECTED";
}else{
$selected = "";
}
}else{
$selected = "";
}
?>
<? echo $selected ?>><? echo
stripslashes($search_result['conference_name'])?> - $<? echo
$result[0]?>
<? } ?>
</select>
</TD>
<TD>
Quantity:
<INPUT
TYPE="text"
NAME="quantity"
SIZE="1"
MAXLENGTH="5"
VALUE="<? if ($notall == 1){echo
stripslashes($quantity);} ?>">
</TD>
</TR>
<?
//After a conference is selected, build drop-downs based on the conference
chosen
if ($conference_cd){
$query = "SELECT * FROM conference_component_t WHERE conference_cd
= $conference_cd AND (retired_flag is NULL OR retired_flag = 0) AND
component_cd != 1 ORDER BY conference_component_name";
$search_results = mysql_query($query) or die("Select Failed!");
?>
<?
while ($search_result = mysql_fetch_array($search_results)) {
?>
<TR>
<TD NOWRAP>
<?if ($search_result['conference_component_name'] != "") {?>
<?echo
stripslashes($search_result['conference_component_name'])?>
<?}else{?>
<? $subquery2 = "SELECT conference_name FROM conference_t
WHERE
conference_cd = $search_result[conference_cd]";
$lists = mysql_query($subquery2) or die("To edit a Conference,
you must
choose one from the list provided.");
$list = mysql_fetch_array($lists);?>
<?echo stripslashes(($list[0])); ?>
<? $query2 = "SELECT component_name FROM component_t WHERE
component_cd = $search_result[component_cd]";
$lists = mysql_query($query2) or die("To edit a Conference,
you must
choose one from the list provided.");
$list = mysql_fetch_array($lists);?>
<?echo stripslashes(($list[0])); ?>
<?}?>
</td>
<td ALIGN=center>
Price: $<? echo
$search_result['conference_component_price']?>
</td>
<td>
//???? How can I differentiate between all of the different component
quantities?
//I'm not sure what to name them in order to pull them out later and save
them
//or to re-display them when someone doesn't fill in all required fields
Quantity: <INPUT
TYPE="text"
NAME="<? //??????? ?>"
SIZE="1"
MAXLENGTH="5"
VALUE="<? if ($notall == 1){echo
stripslashes($quantity);} ?>">
</td>
</TR>
<?
}
?>
<?}?>
<TR>
<TD VALIGN=TOP>
Join these list servs:
</TD>
<TD VALIGN=TOP>
<?
//dynamically create a list of checkboxes
$query = "SELECT * FROM list_serv_t WHERE retired_flag is NULL OR
retired_flag = 0 ORDER BY list_serv_name";
$search_results = mysql_query($query) or die("Select Failed!"); ?>
<?while ($search_result = mysql_fetch_array($search_results)) {
//???? What should I name the checkboxes in order to find the value later?
//???? How do I get the value out??>
<INPUT
TYPE="checkbox"
NAME=""
SIZE="20"
MAXLENGTH="50"
VALUE="1"><? echo
$search_result['list_serv_name'] ?> <BR>
<?
if($notall == 1){
//???? If they didn't answer all the required fields, how do I find the
values entered for the //checkboxes?
}
?>
<?}?>
</TD>
<TD VALIGN=TOP>
Your institution:
</TD>
<TD VALIGN=TOP>
<?$query = "SELECT * FROM org_type_t WHERE retired_flag is NULL OR
retired_flag = 0 ORDER BY org_type_name";
$search_results = mysql_query($query) or die("Select Failed!"); ?>
<?while ($search_result = mysql_fetch_array($search_results)) {?>
<INPUT
TYPE="checkbox"
NAME="<? echo $search_result['org_type_cd']; ?>"
SIZE="20"
MAXLENGTH="50"
VALUE="1">
<?
if ($notall == 1){
//Here, I tried building a variable to hold the variable name of the
checkboxes, but it never seemed to //work, despite countless tweeking
echo "is 1";
$var = "$";
$var .= $search_result['org_type_cd'];
echo $var;
echo $$var;
if ($$var == 1){?>
CHECKED>
<?} else{?>
>
<?}
}else{
echo "not 1";
} ?>
<? echo $var; ?>
<? echo
$search_result['org_type_name'] ?> <BR>
<?}?>
</TD>
</TR>
<TR>
<TD COLSPAN=4>
<TABLE WIDTH='100%'>
<tr bgcolor="brown"><TD align=center><font
color='white'><B>Click Below to Send
Order</B></font></TD></TR>
</TABLE>
</TD>
</TR>
<TR>
<TD COLSPAN=4 ALIGN=CENTER>
<INPUT TYPE="submit" VALUE="Add" name="add">
</TD>
</TR>
</TABLE>
</FORM>