472,953 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,953 software developers and data experts.

Problem in creating 3 level dynamic list box in PHP

Hi, guys:

I have written a piece of code which utilizes Javascript in PHP to create a three level dynamic list box(Country, States/Province, Market). However, I have encountered a strange problem, and I have spent three days trying to debug but to no avail.

Everything is OK when there are only two dependent list boxes, but when adding the third child list box, a problem appears: if I populate the third box only with the value: new Option("marketID"), everything works fine; if I populate the third list box with text: new Option("marketName") or text+value: new Option("marketID", "marketName"), all three list box stop responding to events(onChange).

What is going on? Any suggestions welcome! Thanks a lot.

Here is the PHP function to create the 3 level listbox (please disregard the three $sel....... parameters for now, they are suppose to be defaults):

function Three_Select($country, $province, $market, $selCountry,$selProvince, $selMarket, $result)
{
$sJavaScript='countryselected';
$mJavaScript='regionselected';
$tJavaScript=$mJavaScript;

//Country select box
echo "<TD><SELECT NAME='".$country."' SIZE=\"1\" onChange='".$sJavaScript."(this)' >\n";

//function that populates the province select box
$sJavaScript ="function ".$sJavaScript."(elem){\n ".$test1."for \n (var i = document.myform.".$province.".options.length; i >= 0; i--)
{\n document.myform.".$province.".options[i] = null;\n";

//function that populates the market select box
$mJavaScript ="function ".$mJavaScript."(elem){\n ".$test2."for (var i = document.myform.".$market.".options.length; i >= 0; i--)
{\n document.myform.".$market.".options[i] = null;\n";

$sLastCountry="";
$sLastRegion="";

while ( $row = mysqli_fetch_array($result, MYSQLI_ASSOC) )
{
// is this a new country?
If ($sLastCountry!=$row["CountryName"])
{

// if yes, add the entry to the country's listbox
$sLastCountry = $row["CountryName"];
//if edit data then pre-select country
if($selCountry==$sLastCountry)
{
echo "\n<OPTION SELECTED value='".$row["CountryID"]."'>".$selCountry."</OPTION>";
}
else
echo "\n<OPTION value='".$row["CountryID"]."'>".$sLastCountry."</OPTION>";

// and add a new section to the javascript...
$sJavaScript = $sJavaScript."}\n"."if (elem.value=="
.$row["CountryID"]."){\n";
}

if($sLastRegion!=$row["RegionName"])
{
$sLastRegion = $row["RegionName"];

// and add a new region line to the javascript
$sJavaScript = $sJavaScript."document.myform.".$province.".
options[document.myform.".$province.".options.length]
= new Option('".$row["RegionName"]."', '".$row["RegionID"]."');\n";

$mJavaScript=$mJavaScript."}\n"."if (elem.value=="
.$row["RegionID"]."){\n";
}

/****************************** here is the problematic code ****************/
// and add a new region line to the javascript
$mJavaScript = $mJavaScript."document.myform.".$market.".
options[document.myform.".$market.".options.length]
new Option('".$row["marketName"]."', '".$row["marketID"]."');\n";
/****************************** here is the problematic code ****************/


// $mJavaScript = $mJavaScript."alert('elem value: ' + elem.value + 'function regionselected');\n";
}

// finish the country's listbox; if edit data then pre-select region
if($selProvince!= '')
{
echo "</SELECT></TD>";
echo "<TD><SELECT NAME='".$province."' SIZE=1 onChange='".$tJavaScript."(this)'>\n";
echo "<OPTION SELECTED>".$selProvince."</OPTION>";
echo "</SELECT></TD>";
}
else
{
echo "</SELECT></TD>";
echo "<TD><SELECT NAME='".$province."' SIZE=1 onChange='".$tJavaScript."(this)'>\n";
echo "<OPTION>-- Choose --</OPTION>";
echo "</SELECT></TD>";
}
// finish the country's listbox; if edit data then pre-select region
if($selMarket!= '')
{
echo "<TD colspan='3'><SELECT NAME='".$market."' SIZE=1>";
echo "<OPTION SELECTED>".$selMarket."</OPTION>";
echo "</SELECT></TD>";
}
else
{
echo "<TD colspan='3'><SELECT NAME='".$market."' SIZE=1>";
echo "<OPTION>-- Choose --</OPTION>";
echo "</SELECT></TD>";
}

$sJavaScript = $sJavaScript."\n}\n}\n";
$mJavaScript = $mJavaScript."\n}\n}\n";
echo "\n<SCRIPT LANGUAGE=\"JavaScript\">";
echo "\n".$sJavaScript."\n".$mJavaScript."\n</SCRIPT>\n";
}
The $result is coming from this sql:

//get coutry and region information, for creating list boxes
$sql="SELECT c.CountryName,c.CountryID, r.RegionName, r.RegionID, t.marketID, t.marketName
FROM country as c, region as r, market_tv as t, market_tv_map as m
WHERE r.CountryID=c.CountryID
and r.RegionID=m.regionID
and m.marketID=t.marketID
order by c.CountryID, r.RegionID, t.marketName";

$result = safe_query($link, $sql);
this is the displayed web page source file with both "marketID" and "marketName". All three list boxes do not respond to "onChange" or "onClick" events.
</table><table class="frm2"><tr><td class="lead">*Service Region:</td><TD><SELECT NAME='CountryA' SIZE="1" onChange='countryselected(this)' >

<OPTION value='1'>Canada</OPTION>
<OPTION value='2'>United States</OPTION></SELECT></TD><TD><SELECT NAME='ProvinceA' SIZE=1 onChange='regionselected(this)'>
<OPTION>-- Choose --</OPTION></SELECT></TD><TD colspan='3'><SELECT NAME='MarketA' SIZE=1><OPTION>-- Choose --</OPTION></SELECT></TD>

<SCRIPT LANGUAGE="JavaScript">
function countryselected(elem){
for
(var i = document.myform.ProvinceA.options.length; i >= 0; i--)
{
document.myform.ProvinceA.options[i] = null;
}
if (elem.value==1){
document.myform.ProvinceA.
options[document.myform.ProvinceA.options.length]
= new Option('National', '1');
document.myform.ProvinceA.
options[document.myform.ProvinceA.options.length]
= new Option('Alberta', '2');
...........................
cut away some data
...........................
document.myform.ProvinceA.
options[document.myform.ProvinceA.options.length]
= new Option('Yukon', '14');
}
if (elem.value==2){
document.myform.ProvinceA.
options[document.myform.ProvinceA.options.length]
= new Option('National', '15');
document.myform.ProvinceA.
options[document.myform.ProvinceA.options.length]
= new Option('Alabama', '16');
............................
cut away some data
............................
document.myform.ProvinceA.
options[document.myform.ProvinceA.options.length]
= new Option('Wyoming', '64');
document.myform.ProvinceA.
options[document.myform.ProvinceA.options.length]
= new Option('New York', '65');

}
}

function regionselected(elem){
for (var i = document.myform.MarketA.options.length; i >= 0; i--)
{
document.myform.MarketA.options[i] = null;
}
if (elem.value==1){
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('National', '1');
}
if (elem.value==2){
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('Calgary Lethbridge', '7');
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('Edmonton', '6');
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('Lloydminster (AB/SK)', '41');
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('Medicine Hat', '40');
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('Red Deer', '28');
}
...............................
cut away some data
...............................
if (elem.value==65){
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('Casper', '246');
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('Cheyenne', '244');

}
}

</SCRIPT>
This is the web page source file with only "marketID" in new Option(). All three list boxes respond to "onChange" and "onClick" events:

</table><table class="frm2"><tr><td class="lead">*Service Region:</td><TD><SELECT NAME='CountryA' SIZE="1" onChange='countryselected(this)' >

<OPTION value='1'>Canada</OPTION>
<OPTION value='2'>United States</OPTION></SELECT></TD><TD><SELECT NAME='ProvinceA' SIZE=1 onChange='regionselected(this)'>
<OPTION>-- Choose --</OPTION></SELECT></TD><TD colspan='3'><SELECT NAME='MarketA' SIZE=1><OPTION>-- Choose --</OPTION></SELECT></TD>

<SCRIPT LANGUAGE="JavaScript">
function countryselected(elem){
for
(var i = document.myform.ProvinceA.options.length; i >= 0; i--)
{
document.myform.ProvinceA.options[i] = null;
}
if (elem.value==1){
document.myform.ProvinceA.
options[document.myform.ProvinceA.options.length]
= new Option('National', '1');
document.myform.ProvinceA.
options[document.myform.ProvinceA.options.length]
= new Option('Alberta', '2');
..................................
cut away some data
..................................
document.myform.ProvinceA.
options[document.myform.ProvinceA.options.length]
= new Option('Yukon', '14');
}
if (elem.value==2){
document.myform.ProvinceA.
options[document.myform.ProvinceA.options.length]
= new Option('National', '15');
document.myform.ProvinceA.
options[document.myform.ProvinceA.options.length]
= new Option('Alabama', '16');
.........................................
cut away some data
........................................
document.myform.ProvinceA.
options[document.myform.ProvinceA.options.length]
= new Option('New York', '65');

}
}

function regionselected(elem){
for (var i = document.myform.MarketA.options.length; i >= 0; i--)
{
document.myform.MarketA.options[i] = null;
}
if (elem.value==1){
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('1');
}
if (elem.value==2){
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('7');
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('6');
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('41');
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('40');
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('28');
}
........................................
cut away some data
........................................
if (elem.value==65){
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('246');
document.myform.MarketA.
options[document.myform.MarketA.options.length]
= new Option('244');

}
}

</SCRIPT>
Please help!!
Feb 5 '08 #1
1 3689
ronverdonk
4,258 Expert 4TB
This bunch of unstructured code is absolutely unreadable. It makes your chance of it being looked at low.

So please enclose any code within the proper code tags. See the Posting Guidelines on how to do that.

moderator
Feb 24 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: André | last post by:
Hi, is it possible to override methods for one specific object, like you can do in Ruby? Something like: --8<-- class A { public: void method();
3
by: Craig Jurney | last post by:
Am having difficulty creating a dynamic <select> element using direct assignment to the element's option array (ie. someElement.option=new Option(someText, someValue);) that will work on Palm...
3
by: Kris van der Mast | last post by:
Hi, I've created a little site for my sports club. In the root folder there are pages that are viewable by every anonymous user but at a certain subfolder my administration pages should be...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
4
by: snowweb | last post by:
I am trying to implement a CSS hierarchical unfolding menu on a site. The thing is, it needs to be dynamically populated from the results of a database query. I previously had the menu working but...
5
by: m0002a | last post by:
Is there some way to track the isolation level of an indivual SQL statement submitted via JDBC in a snaphot or some other similar means? I have JDBC programs that are changing the isolation level...
4
by: Spectre1337 | last post by:
Hello, I'm having huge difficulties solving what should be a relatively trivial problem. The following is a gross simplification (obviously it's not that simple in reality) but it will serve its...
3
by: jaddi1 | last post by:
Hi, I am trying to make a multi-level drop-down menu similar to what is seen here: http://www.cssplay.co.uk/menus/simple_vertical.html. My problem is that some of the menu will be populated from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.