Forums

PHP

This topic is locked

dependant dynamic dropdown lists in PHP/MySQL

Posted 19 Jun 2003 00:33:42
1
has voted
19 Jun 2003 00:33:42 waleed ware posted:
Hi

I want please to know how to do the same thing in this ultradev tutorial but using PHP/MySQL/Javascript , as this tutorial uses Vbscript/Javascript in ASP pages

www.macromedia.com/support/ultradev/ts/documents/client_dynamic_listbox.htm

Please i need an answer as soon as possible

Thank you

Replies

Replied 19 Jun 2003 01:43:57
19 Jun 2003 01:43:57 waleed ware replied:
Here is the code generated by DWMX , i think i need to change something here , as the parent list is populated correctley by the recordset , but on choosing one category on the parent list the child list did not change or get populated by the recordset for the corresponding parent category.

<?php require_once('Connections/connLogbook.php'); ?>
<?php
mysql_select_db($database_connLogbook, $connLogbook);
$query_rsType = "SELECT * FROM atype";
$rsType = mysql_query($query_rsType, $connLogbook) or die(mysql_error());
$row_rsType = mysql_fetch_assoc($rsType);
$totalRows_rsType = mysql_num_rows($rsType);

mysql_select_db($database_connLogbook, $connLogbook);
$query_rsAreg = "SELECT * FROM areg";
$rsAreg = mysql_query($query_rsAreg, $connLogbook) or die(mysql_error());
$row_rsAreg = mysql_fetch_assoc($rsAreg);
$totalRows_rsAreg = mysql_num_rows($rsAreg);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- Dynamic Dependent List box Code for *** JavaScript *** Server Model //-->
<script language="JavaScript">
<!--

var arrDynaList = new Array();
var arrDL1 = new Array();

arrDL1[1] = "atype"; // Name of parent list box
arrDL1[2] = "form1"; // Name of form containing parent list box
arrDL1[3] = "areg"; // Name of child list box
arrDL1[4] = "form1"; // Name of form containing child list box
arrDL1[5] = arrDynaList;

<%
var txtDynaListRelation, txtDynaListLabel, txtDynaListValue, oDynaListRS;

txtDynaListRelation = "atypeID" // Name of recordset field relating to parent
txtDynaListLabel = "areg" // Name of recordset field for child Item Label
txtDynaListValue = "aregID" // Name of recordset field for child Value
oDynaListRS = rsAreg // Name of child list box recordset

var varDynaList = -1;
var varMaxWidth = "10";
var varCheckGroup = oDynaListRS.Fields.Item(txtDynaListRelation).Value;
var varCheckLength = 0;
var varMaxLength = 0;

while (!oDynaListRS.EOF){

if (varCheckGroup != oDynaListRS.Fields.Item(txtDynaListRelation).Value) {
varMaxLength = Math.max(varCheckLength, varMaxLength)
varCheckLength = 0;
}
%>
arrDynaList[<%=(varDynaList+1)%>] = "<%=(oDynaListRS.Fields.Item(txtDynaListRelation).Value)%>";
arrDynaList[<%=(varDynaList+2)%>] = "<%=(oDynaListRS.Fields.Item(txtDynaListLabel).Value)%>";
arrDynaList[<%=(varDynaList+3)%>] = "<%=(oDynaListRS.Fields.Item(txtDynaListValue).Value)%>";
<%

if (oDynaListRS.Fields.Item(txtDynaListLabel).Value.length > varMaxWidth.length) {
varMaxWidth = oDynaListRS.Fields.Item(txtDynaListLabel).Value;
}
varCheckLength = varCheckLength + 1;
varDynaList = varDynaList + 3;
oDynaListRS.MoveNext();
}
varMaxLength = Math.max(varCheckLength, varMaxLength)
%>


//-->
</script>

<!-- End of object/array definitions, beginning of generic functions -->

<script language="JavaScript">
<!--
function setDynaList(arrDL){

var oList1 = document.forms[arrDL[2]].elements[arrDL[1]]
var oList2 = document.forms[arrDL[4]].elements[arrDL[3]]
var arrList = arrDL[5]

clearDynaList(oList2);

if (oList1.selectedIndex == -1){
oList1.selectedIndex = 0;
}

populateDynaList(oList2, oList1[oList1.selectedIndex].value, arrList);
return true;
}

function clearDynaList(oList){

for (var i = oList.options.length; i >= 0; i--){
oList.options[i] = null;
}

oList.selectedIndex = -1;
}

function populateDynaList(oList, nIndex, aArray){

for (var i = 0; i < aArray.length; i= i + 3){
if (aArray[i] == nIndex){
oList.options[oList.options.length] = new Option(aArray[i + 1], aArray[i + 2]);
}
}

if (oList.options.length == 0){
oList.options[oList.options.length] = new Option("[none available]",0);
}

oList.selectedIndex = 0;
}

function MM_callJS(jsStr) { //v2.0
return eval(jsStr)
}
//-->
</script>
</head>

<body onLoad="MM_callJS('setDynaList(arrDL1)')">
<form name="form1" method="post" action="">
<select name="atype" id="atype" onChange="MM_callJS('setDynaList(arrDL1)')">
<?php
do {
?>
<option value="<?php echo $row_rsType['atypeID']?>"><?php echo $row_rsType['atype']?></option>
<?php
} while ($row_rsType = mysql_fetch_assoc($rsType));
$rows = mysql_num_rows($rsType);
if($rows > 0) {
mysql_data_seek($rsType, 0);
$row_rsType = mysql_fetch_assoc($rsType);
}
?>
</select>
<select name="areg" id="areg">
</select>
</form>
</body>
</html>
<?php
mysql_free_result($rsType);

mysql_free_result($rsAreg);
?>
Replied 01 Feb 2006 15:50:17
01 Feb 2006 15:50:17 P O replied:
Hi!

I used your script but have taken away one of the two dropdownlists. (Didnt need it).

When I now have released the website I got this error.
Maybe I have missed to taken away something.

Please help.
//PO

This is the error message:
Parse error: syntax error, unexpected T_VAR in /secret/ap_profiles_aaa.php on line 24

---------------------------------------
This is the script changed to fit our service:


<?php require_once('Connections/conn_secret.php'); ?>
<?php
mysql_select_db($database_conn_secret_airports, $conn_secret_airports);
$query_rs_flighttourNames = "SELECT tbl_airport.airportID, tbl_airport.airportName, tbl_airport.icaoDesignator FROM tbl_airport ORDER BY tbl_airport.airportName";
$rs_flighttourNames = mysql_query($query_rs_flighttourNames, $conn_secret_airports) or die(mysql_error());
$row_rs_flighttourNames = mysql_fetch_assoc($rs_flighttourNames);
$totalRows_rs_flighttourNames = mysql_num_rows($rs_flighttourNames);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script language="JavaScript">
<!--

var arrDynaList = new Array();
var arrDL1 = new Array();

arrDL1[1] = "atype"; // Name of parent list box
arrDL1[2] = "form1"; // Name of form containing parent list box
arrDL1[3] = arrDynaList;

<%
var txtDynaListRelation = "airportID" // Name of recordset field relating to parent
var varDynaList = -1;
var varMaxWidth = "10";
var varCheckGroup = oDynaListRS.Fields.Item(txtDynaListRelation).Value;
var varCheckLength = 0;
var varMaxLength = 0;

while (!oDynaListRS.EOF){

if (varCheckGroup != oDynaListRS.Fields.Item(txtDynaListRelation).Value) {
varMaxLength = Math.max(varCheckLength, varMaxLength)
varCheckLength = 0;
}
%>
arrDynaList[<%=(varDynaList+1)%>] = "<%=(oDynaListRS.Fields.Item(txtDynaListRelation).Value)%>";
<%

if (oDynaListRS.Fields.Item(txtDynaListLabel).Value.length > varMaxWidth.length) {
varMaxWidth = oDynaListRS.Fields.Item(txtDynaListLabel).Value;
}
varCheckLength = varCheckLength + 1;
varDynaList = varDynaList + 3;
oDynaListRS.MoveNext();
}
varMaxLength = Math.max(varCheckLength, varMaxLength)
%>


//-->
</script>

<!-- End of object/array definitions, beginning of generic functions -->

<script language="JavaScript">
<!--
function setDynaList(arrDL){

var oList1 = document.forms[arrDL[2]].elements[arrDL[1]]
var arrList = arrDL[3]

clearDynaList(oList2);

if (oList1.selectedIndex == -1){
oList1.selectedIndex = 0;
}

populateDynaList(oList2, oList1[oList1.selectedIndex].value, arrList);
return true;
}

function clearDynaList(oList){

for (var i = oList.options.length; i >= 0; i--){
oList.options[i] = null;
}

oList.selectedIndex = -1;
}

function populateDynaList(oList, nIndex, aArray){

for (var i = 0; i < aArray.length; i= i + 3){
if (aArray[i] == nIndex){
oList.options[oList.options.length] = new Option(aArray[i + 1], aArray[i + 2]);
}
}

if (oList.options.length == 0){
oList.options[oList.options.length] = new Option("[none available]",0);
}

oList.selectedIndex = 0;
}

function MM_callJS(jsStr) { //v2.0
return eval(jsStr)
}
//-->
</script>
<link href="corporatestyle.css" rel="stylesheet" type="text/css" />
</head>
<body onLoad="MM_callJS('setDynaList(arrDL1)')">
<form name="form1" method="post" action="">
<?php do { ?>
<select name="atype" class="searchform" id="atype" onChange="MM_callJS('setDynaList(arrDL1)')">
<option value="<?php echo $row_rs_flighttourNames['airportID']?>"></option>
<?php
} while ($row_rs_flighttourNames = mysql_fetch_assoc($rs_flighttourNames));
$rows = mysql_num_rows($rs_flighttourNames);
if($rows > 0) {
mysql_data_seek($rs_flighttourNames, 0);
$row_rs_flighttourNames = mysql_fetch_assoc($rs_flighttourNames);
}
?>
</select>
<?php } while ($row_rs_flighttourNames = mysql_fetch_assoc($rs_flighttourNames)); ?>
</form>
</body>
</html>
<?php
mysql_free_result($rs_flighttourNames);
?>
Replied 13 Jul 2007 05:23:09
13 Jul 2007 05:23:09 Mike Freeman replied:
Hi,
Put a semicolon ( ; ) after "airportID" in line 24 and it should fix the unexpected T_VAR problem. It's been over a year, but hopefully you or someone else can use the info <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>


Mike Freeman
NewMP3Technology
www.newmp3technology.com

Reply to this topic