473,756 Members | 5,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem in creating 3 level dynamic list box in PHP

2 New Member
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("marketI D"), everything works fine; if I populate the third list box with text: new Option("marketN ame") or text+value: new Option("marketI D", "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($c ountry, $province, $market, $selCountry,$se lProvince, $selMarket, $result)
{
$sJavaScript='c ountryselected' ;
$mJavaScript='r egionselected';
$tJavaScript=$m JavaScript;

//Country select box
echo "<TD><SELEC T NAME='".$countr y."' SIZE=\"1\" onChange='".$sJ avaScript."(thi s)' >\n";

//function that populates the province select box
$sJavaScript ="function ".$sJavaScript. "(elem){\n ".$test1."f or \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."f or (var i = document.myform .".$market.".op tions.length; i >= 0; i--)
{\n document.myform .".$market.".op tions[i] = null;\n";

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

while ( $row = mysqli_fetch_ar ray($result, MYSQLI_ASSOC) )
{
// is this a new country?
If ($sLastCountry! =$row["CountryNam e"])
{

// if yes, add the entry to the country's listbox
$sLastCountry = $row["CountryNam e"];
//if edit data then pre-select country
if($selCountry= =$sLastCountry)
{
echo "\n<OPTION SELECTED value='".$row["CountryID"]."'>".$selCount ry."</OPTION>";
}
else
echo "\n<OPTION value='".$row["CountryID"]."'>".$sLastCou ntry."</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."d ocument.myform. ".$province .".
options[document.myform .".$province.". options.length]
= new Option('".$row["RegionName "]."', '".$row["RegionID"]."');\n";

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

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


// $mJavaScript = $mJavaScript."a lert('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><SELEC T NAME='".$provin ce."' SIZE=1 onChange='".$tJ avaScript."(thi s)'>\n";
echo "<OPTION SELECTED>".$sel Province."</OPTION>";
echo "</SELECT></TD>";
}
else
{
echo "</SELECT></TD>";
echo "<TD><SELEC T NAME='".$provin ce."' SIZE=1 onChange='".$tJ avaScript."(thi s)'>\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'><SE LECT NAME='".$market ."' SIZE=1>";
echo "<OPTION SELECTED>".$sel Market."</OPTION>";
echo "</SELECT></TD>";
}
else
{
echo "<TD colspan='3'><SE LECT 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=\"Java Script\">";
echo "\n".$sJavaScri pt."\n".$mJavaS cript."\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.C ountryID
and r.RegionID=m.re gionID
and m.marketID=t.ma rketID
order by c.CountryID, r.RegionID, t.marketName";

$result = safe_query($lin k, $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"><t r><td class="lead">*S ervice Region:</td><TD><SELECT NAME='CountryA' SIZE="1" onChange='count ryselected(this )' >

<OPTION value='1'>Canad a</OPTION>
<OPTION value='2'>Unite d States</OPTION></SELECT></TD><TD><SELECT NAME='ProvinceA ' SIZE=1 onChange='regio nselected(this) '>
<OPTION>-- Choose --</OPTION></SELECT></TD><TD colspan='3'><SE LECT NAME='MarketA' SIZE=1><OPTION>-- Choose --</OPTION></SELECT></TD>

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

}
}

function regionselected( elem){
for (var i = document.myform .MarketA.option s.length; i >= 0; i--)
{
document.myform .MarketA.option s[i] = null;
}
if (elem.value==1) {
document.myform .MarketA.
options[document.myform .MarketA.option s.length]
= new Option('Nationa l', '1');
}
if (elem.value==2) {
document.myform .MarketA.
options[document.myform .MarketA.option s.length]
= new Option('Calgary Lethbridge', '7');
document.myform .MarketA.
options[document.myform .MarketA.option s.length]
= new Option('Edmonto n', '6');
document.myform .MarketA.
options[document.myform .MarketA.option s.length]
= new Option('Lloydmi nster (AB/SK)', '41');
document.myform .MarketA.
options[document.myform .MarketA.option s.length]
= new Option('Medicin e Hat', '40');
document.myform .MarketA.
options[document.myform .MarketA.option s.length]
= new Option('Red Deer', '28');
}
............... ............... .
cut away some data
............... ............... .
if (elem.value==65 ){
document.myform .MarketA.
options[document.myform .MarketA.option s.length]
= new Option('Casper' , '246');
document.myform .MarketA.
options[document.myform .MarketA.option s.length]
= new Option('Cheyenn e', '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"><t r><td class="lead">*S ervice Region:</td><TD><SELECT NAME='CountryA' SIZE="1" onChange='count ryselected(this )' >

<OPTION value='1'>Canad a</OPTION>
<OPTION value='2'>Unite d States</OPTION></SELECT></TD><TD><SELECT NAME='ProvinceA ' SIZE=1 onChange='regio nselected(this) '>
<OPTION>-- Choose --</OPTION></SELECT></TD><TD colspan='3'><SE LECT NAME='MarketA' SIZE=1><OPTION>-- Choose --</OPTION></SELECT></TD>

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

}
}

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

}
}

</SCRIPT>
Please help!!
Feb 5 '08 #1
1 3752
ronverdonk
4,258 Recognized Expert Specialist
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
1753
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
1610
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 devices runing Blazer 3.0 or Web Browser 2.0. The browsers SEEM to be JavaScript1.1+ capable and the script that I have works on every desktop-based browser, but not the PDA ones.... Any advice? Or is this a known deficiency?
3
4870
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 protected by forms authentication. When I create forms authentication at root level it works but when I move my code up to the subfolder I get this error: Server Error in '/TestProjects/FormsAuthenticationTestingArea' Application.
10
4029
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 application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
2
4453
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 */ #include <time.h>
4
19111
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 then it was ‘hard coded’ and not built on the fly. Menu description: 2 top level items “Company” and “Products” (we will ignore “Company” since it is still hard coded and not causing a problem. Below “Products” we have hard coded “By...
5
3350
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 via Connection.setTransactionIsolation(int level) method, such as TRANSACTION_SERIALIZABLE or TRANSACTION_REPEATABLE_READ and I need to verify that in DB2 at a SQL statement level. I don't see isolation level in a snapshot for Dynamic SQL.
4
1974
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 purpose: I need to program a dynamically generated list, kind of like a shoutbox, that stores the messages in a Profile variable. I know, I know, the messages would be lost as soon as the session expires, but that'll be sufficient for this...
3
4611
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 a database. My menu will be structured as follows: The first setion of the menu is hard-coded (the home and management sections). The rest of the menu will be populated from a database. The top level links (categories) will come from one table...
0
9454
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10028
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9836
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9707
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6533
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5139
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.