473,598 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dependable select boxes

Hello there...

I'm a PHP programmer and starting to learn JS...
I have a following problem....

I have 3 select boxes! one is hotel one is destination and one is
country...

if someone clicks selects the country then the destination select box shows
the destinations in that country and further if he chooses destination all
the hotels in in that destination are shown in hotel select
box....(everyth ing is from mysql database)

I don't have a clue how to do this or where to find some readings about it
and I really need that desperatly....

Thanx for any infoes...

Point
Jul 20 '05 #1
4 6425
"point" <po***@caanprod uction.com> writes:
I have 3 select boxes! one is hotel one is destination and one is
country...

if someone clicks selects the country then the destination select box shows
the destinations in that country and further if he chooses destination all
the hotels in in that destination are shown in hotel select
box....(everyth ing is from mysql database)

I don't have a clue how to do this or where to find some readings about it
and I really need that desperatly....


This is a very common "problem" (it is not really a problem, it's
quite easy to fix).

<FAQENTRY>
You can add options to a select element using "selectRef. add" and
remove them with "selectRef.remo ve", although these functions are not
present in older browsers.

You need to keep the new data somewhere else, probably as an array of
text/value pairs.

The following function changes the options of a select element:
---
function setOptions(sele ctRef,optArray) {
var optsRef = selectRef.optio ns;
// Clear old options
optsRef.length = 0;
// Insert new options
for (var i = 0 ; i < optArray.length-1 ; i += 2) {
var opt = new Option(optArray[i],optArray[i+1]); // text,value
optsRef[optsRef.length] = opt;
}
}
---
You can use this function from the onchange handler of another select element.
</FAQENTRY>

You need data like:
---
var data = new Array();
data[0] = new Array("Nothing" ,"");
data[1] = new Array("text A1","value A1","text A2","value A2");
data[2] = new Array("text B1","value B1","text B2","value B2");
---
and code like:
---
<form ...>
<select
onchange="setOp tions(this.form .elements['select2'],data[this.selectedIn dex])">
<option selected="selec ted">Nothing</option>
<option>A Options</option>
<option>B Options</option>
</select>
<select name="select2">
<option value="">Nothin g</option>
</select>
</form>
---

You *MUST* be aware that your page will not work for people without
javascript (either not available or turned off). You could start
out with populating your selects with all possible options, and then
use the above to set only those hotels that are in the correct
country, after the page has loaded. Then people without javascript can
still use the page, just not as easily.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:pt******** **@hotpop.com.. .
<snip>
You need to keep the new data somewhere else, probably
as an array of text/value pairs.

<snip>

I have never liked solutions to this problem that use JavaScript arrays
of name value pairs. The result is always dependant on JavaScript and
thus will not be usable if JavaScript is not available. If the data
(text/value pairs) is defined in the HTML as options in a very long
select list (with JavaScript recording the values before removing the
unwanted ones) or as multiple select elements with a list each (in which
case JavaScript would hide the unwanted select elements and swap them
when needed) then the form could remain usable (if confusing) in the
absence of JavaScript.

A more robust alternative might be to re-work the form into a "wizard"
style interface, with each dependant select element being created
server-side based on the results from the submission of the previous
page.

Richard.
Jul 20 '05 #3
I like the solution below. You could also use the onChange event of the
select boxes to submit the form and then recalculate what should be in the
boxes accordingly. Separate screens would probably be a better design.

P.

"Richard Cornford" <ri*****@litote s.demon.co.uk> wrote in message
news:bf******** **@titan.btinte rnet.com...
"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:pt******** **@hotpop.com.. .
<snip>
You need to keep the new data somewhere else, probably
as an array of text/value pairs.

<snip>

I have never liked solutions to this problem that use JavaScript arrays
of name value pairs. The result is always dependant on JavaScript and
thus will not be usable if JavaScript is not available. If the data
(text/value pairs) is defined in the HTML as options in a very long
select list (with JavaScript recording the values before removing the
unwanted ones) or as multiple select elements with a list each (in which
case JavaScript would hide the unwanted select elements and swap them
when needed) then the form could remain usable (if confusing) in the
absence of JavaScript.

A more robust alternative might be to re-work the form into a "wizard"
style interface, with each dependant select element being created
server-side based on the results from the submission of the previous
page.

Richard.

Jul 20 '05 #4
Ok....I send my answer to Lasse by accidentaly clicking Reply instead of
ReplyGroup....

I asked if he can do the script for three select's and here is his code:
he did it with and withouth option buttons....

The code is a bit hard aand after whole night I still don't have a clue on
how to use it as the records are from the database...

So if anyone is willing to help I can post the TABLEs structure....

One again thanx Lasse...

by Lasse Reichstein Nielsen:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">

<html> <head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Hotel Selection</title>

<script type="text/javascript">

var CountryCity = new Object();

CountryCity["CtryAustri a"] = new Array("Vienna", "CityVienna ",

"SalzBurg", "CitySalzburg") ;

CountryCity["CtryCroati a"] = new Array("Zagreb", "CityZagreb ",

"Split", "CitySplit" )
var CityHotel = new Object();

CityHotel["CityVienna "] = new Array("Palace", "HotelPalac e",

"Royal", "HotelRoyal ");

CityHotel["CitySalzbu rg"] = new Array("Sheraton ", "HotelSheraton" ,

"Lukas", "HotelLukas ");

CityHotel["CityZagreb "] = new Array("Opera", "HotelOpera ",

"Conti", "HotelConti ");

CityHotel["CitySplit"] = new Array("Inter", "HotelInter ",

"Unter", "HotelUnter ");
function setOptions(sele ctRef,optArray) {

var optsRef = selectRef.optio ns;

// Clear old options

if (selectRef.remo veChild) { // DOM to remove optgroups as well as options

while(selectRef .hasChildNodes( )) {

selectRef.remov eChild(selectRe f.firstChild);

}

}

optsRef.length = 0;

// Insert new options

for (var i = 0 ; i < optArray.length-1 ; i += 2) {

var opt = new Option(optArray[i],optArray[i+1]); // text,value

optsRef[optsRef.length] = opt;

}

}

function onChangeCtry(se lRef) {

var value = selRef.options[selRef.selected Index].value;

var citySel = selRef.form.ele ments['citySel'];

setOptions(city Sel,CountryCity[value]);

citySel.options[0].selected = true;

onChangeCity(ci tySel);

}

function onChangeCity(se lRef) {

var value = selRef.options[selRef.selected Index].value;

var hotelSel = selRef.form.ele ments['hotelSel'];

setOptions(hote lSel,CityHotel[value]);

hotelSel.option s[0].selected=true;

}
function init() {

onChangeCtry(do cument.forms['form1'].elements['countrySel']);

onChangeCtry(do cument.forms['form2'].elements['countrySel']);

}

</script>

</head>

<body onload="init(); ">

<h1>Hotel Selection</h1>

<form name="form1" action="">

<p><label for="countrySel ">Country:

<select name="countrySe l" onchange="onCha ngeCtry(this)">

<option value="CtryAust ria">Austria</option>

<option value="CtryCroa tia">Croatia</option>

</select></label>

</p>

<p><label for="citySel">C ity:

<select name="citySel" onchange="onCha ngeCity(this)">

<option value="CityVien na">Vienna</option>

<option value="CitySalz burg">Salzburg</option>

<option value="CityZagr eb">Zagreb</option>

<option value="CitySpli t">Split</option>

</select></label>

</p>

<p><label for="hotelSel"> Hotel:

<select name="hotelSel" >

<option value="HotelPal ace">Palace</option>

<option value="HotelRoy al">Royal</option>

<option value="HotelShe raton">Sheraton </option>

<option value="HotelLuk as">Lukas</option>

<option value="HotelOpe ra">Opera</option>

<option value="HotelCon ti">Conti</option>

<option value="HotelInt er">Inter</option>

<option value="HotelUnt er">Unter</option>

</select></label>

</p>

</form>

<hr>

<p>Same form with option groups</p>

<!-- =============== =============== =============== =============== ===== -->

<form name="form2" action="">

<p><label for="countrySel ">Country:

<select name="countrySe l" onchange="onCha ngeCtry(this)">

<option value="CtryAust ria">Austria</option>

<option value="CtryCroa tia">Croatia</option>

</select></label>

</p>

<p><label for="citySel">C ity:

<select name="citySel" onchange="onCha ngeCity(this)">

<optgroup label="Austria" >

<option value="CityVien na">Vienna</option>

<option value="CitySalz burg">Salzburg</option>

</optgroup>

<optgroup name="Croatia">

<option value="CityZagr eb">Zagreb</option>

<option value="CitySpli t">Split</option>

</optgroup>

</select></label>

</p>

<p><label for="hotelSel"> Hotel:

<select name="hotelSel" >

<optgroup label="Vienna">

<option value="HotelPal ace">Palace</option>

<option value="HotelRoy al">Royal</option>

</optgroup>

<optgroup label="Salzburg ">

<option value="HotelShe raton">Sheraton </option>

<option value="HotelLuk as">Lukas</option>

</optgroup>

<optgroup label="Zagreb">

<option value="HotelOpe ra">Opera</option>

<option value="HotelCon ti">Conti</option>

</optgroup>

<optgroup label="Split">

<option value="HotelInt er">Inter</option>

<option value="HotelUnt er">Unter</option>

</optgroup>

</select></label>

</p>

</form>

<hr>

"ManoDestra " <ma************ *****@ntlworld. com> wrote in message
news:wE******** **********@news fep2-gui.server.ntli .net...
I like the solution below. You could also use the onChange event of the
select boxes to submit the form and then recalculate what should be in the
boxes accordingly. Separate screens would probably be a better design.

P.

"Richard Cornford" <ri*****@litote s.demon.co.uk> wrote in message
news:bf******** **@titan.btinte rnet.com...
"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:pt******** **@hotpop.com.. .
<snip>
You need to keep the new data somewhere else, probably
as an array of text/value pairs.

<snip>

I have never liked solutions to this problem that use JavaScript arrays
of name value pairs. The result is always dependant on JavaScript and
thus will not be usable if JavaScript is not available. If the data
(text/value pairs) is defined in the HTML as options in a very long
select list (with JavaScript recording the values before removing the
unwanted ones) or as multiple select elements with a list each (in which
case JavaScript would hide the unwanted select elements and swap them
when needed) then the form could remain usable (if confusing) in the
absence of JavaScript.

A more robust alternative might be to re-work the form into a "wizard"
style interface, with each dependant select element being created
server-side based on the results from the submission of the previous
page.

Richard.


Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1829
by: Simon Finn | last post by:
Hi I have used ASP code to load data into two select boxes. The code below is used to move the selected data back and forth between the boxes. I am having problems retreving the data from the boxes after the user has moved any of the data: If the data has been moved by the below code; when i try and load the data back into the database i cannot retrieve the data using:
12
8867
by: Forti2ude | last post by:
Hello, I have a simple form... <form> <select name="foo" multiple> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select>
5
2130
by: Allan M. | last post by:
I have a series of select boxes that must be populated client side, because they interact with each other. The design specification calls for these boxes to be updated without having to make a roundtrip to the server. The codebehind seems to be unaware of select box members populated via javascript. So, I'm having to create my own state management solution, (i.e. rewriting the VIEWSTATE mechanism) to persist the state of these select...
5
3687
by: jjyconsulting | last post by:
Newbie needing some help. I have a tblParticipants. The fields include gender, education_level, income, occupation etc., I'm trying to create a form where a user can run a query from the form and just choose the appropriate criterias from the combo boxes to get the results. I also want the query to run even if there is not a value in all the combo boxes ie., i want just all males with income level of over $100,000...Any insights or help...
2
8416
by: simon.wilkinson | last post by:
Hi, I am trying to update all Select boxes on a page dynamically using javascript, I simple want to change the selected item in each select box when a tick box is pressed on the page. Each Select box is named in the same convention ie. ddl_DeliveryStatus_ and then the recordID and contains the same options in the same order. The number of select boxes changes every time the page is loaded as this is all built using ASP linked to a...
14
4875
by: Heggr | last post by:
I have a page that when you click on a button it is supposed to add 5 more rows to an existing table. This functionality works but then we had to change one of the cells from using a Text box to using a Select box and then it no longer worked completely. I can see the Select box with the drop down arrow but the width of the box is not the same as the original Select boxes. Also the OnClick and OnChange events do not work for the newly added...
6
4659
by: woodey2002 | last post by:
Hi Everyone. Thanks for your time. I am trying to create a search form that will allow users to select criteria from multiple multi select boxes. So far i have managed to achieve a search option for 2 list boxes:- county and nationality, while trying to add a third multi select list box for qualifications search is where i encounter my problem. I've copied the working code from my working list boxes, however it cant seem to pick up the...
1
6790
by: woodey2002 | last post by:
Hi Everyone and many thanks for your time.. I am trying to begin access and a bit of VBA i am enjoying it but I have a annoying problem I just can’t get any where on. My databse mostly includes bits of code for different examples. I have one last thing to finish. I am trying to create a search form that will allow users to select criteria from multiple sources eg ,multi select list boxes , combo boxes. I have a subform showing all the...
0
7981
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
8284
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...
0
8392
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3894
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
3938
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2410
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
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.