473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Selecting multiple option from a select tag

Hi, I am kinda new to javascript and I am having this problem with
selecting multiple options from a select tag. Hope someone can help me
out here.

here is my code
///////////////////////////////////////////////////////////////////////////////////////
<form action="whatever.php" method="post">
<select name="zip_code"
onchange="makeRequest('getCity.php?state='+this.fo rm.zip_code.options[this.form.zip_code.selectedIndex].value)"
multiple="multiple" size="20">
<?
while($row = mysql_fetch_array($rs)){
?><option
value="<?=$row['f_fips_code']?>"><?=$row['f_name']?></option><?
}
?>
</select>
<select name="test" id="t1" multiple="multiple" size="40">
<option>Select a state</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
/////////////////////////////////////////////////////////////////////////////////////
Here I need to be able to select multiple options (zip codes) and then
pass those zip codes thru the makeRequest function which then calls a
php page to do a query to my db and returns the state and county under
that zip code. Now my code works fine for single selection but I don't
know how to do multiple selection. I know that if I name my select tag
as zip_code[] instead of zip_code that takes care of multiple selection
but then how should I pass those multiple selections thru the
makeRequest function?

Any help will be highly appreciable...

Thanks
Arif

Feb 19 '06 #1
2 3621
ar*********@gmail.com wrote:
Hi, I am kinda new to javascript and I am having this problem
with selecting multiple options from a select tag. Hope someone
can help me out here.

here is my code
///////////////////////////////////////
<form action="whatever.php" method="post">
<select name="zip_code"
onchange="makeRequest('getCity.php?state='+
If a select box lists zip codes, and the PHP requests returns cites the
name 'state' is an inherently confusing choice to label any transmitted
data in this context.
this.form.zip_code.options[this.form.zip_code.selectedIndex].value)"
In the event handling function that is generated from the string value
of an intrinsic event attribute the - this - keyword is a reference to
the element to which the handler is attached; the select element with
the name 'zip_code' in this case.

The - form - property of that element is a reference to the form, and -
form.zip_code - is a common HTML DOM shortcut reference to the form
control with the name 'zip_code'. That is, - this - refers to the same
element as - this.form.zip_code - in this case, only more directly.
However, the mark-up here appears to be XHTML so in the event that it is
ever interpreted as XHTML and an XHTML DOM created from it to be
scripted (as opposed to an HTML DOM) then a different scripting
environment will apply. One of the many differences between and XHTML
DOM and an HTML DOM is that 'shortcut' and 'convenience' property
accessors that have not been formally specified (in the W3C Level 2 HTML
DOM specification) are not necessarily available for use, even when they
have been common (even universal) in HTML DOM implementations. So using
the 'shortcut' of referring to a named form control as a named property
of the containing form will not necessarily be reliable in XHTML DOM
implementations. The alternative of referring to such controls as named
properties of the form's - elements - collection should always be
available as it is formally specified.

The - selectedIndes - property of a SELECT element is not meaningful in
a multiple select element, and the value of any OPTION element that may
be selected is only ever going to be a single value from any group of
OPTIONs selected.

To find all of the selected OPTION elements in such a SELECT element it
is necessary to loop through the - options - collection of the SELECT
element and respond to each OPTION element's - selected - property
(which is boolean, either true or false). Something like:-

var options = this.options;
for(var c = options.length;--c;){
if( options[c].selected ){ //true if selected.
// do something with the - value of the selected option.
}
}

Exactly what is done with those values depends on what is needed. To
transmit them in the query value of a GET request would involve URL
encoding the values and assembling them into some sort of string that
can be disassembled on the sever to recover the data.
multiple="multiple" size="20">
<?
This is PHP code and so is one step removed from the (x)HTML +
javascript code that the browser receives. If there is a problem with
the client-side code looking at the server side code that generates it
is almost guaranteed to obscure the cause of the client-side problem
even for individuals who know the sever side language. Debugging the
client-side aspects of server generated HTML + javascript is
considerably aided by using the view source facilities on web-browses to
look at the code actually sent to the client, and it is that code that
should be posted to this group for the identification of client-side
issues.
while($row = mysql_fetch_array($rs)){
?><option
value="<?=$row['f_fips_code']?>"><?=$row['f_name']?></option><?
}
?>
</select>
<select name="test" id="t1" multiple="multiple" size="40">
<option>Select a state</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
/////////////////////////////////////////////////////
Here I need to be able to select multiple options (zip codes) and then
pass those zip codes thru the makeRequest function which then calls a
php page to do a query to my db and returns the state and county under
that zip code.

<snip>

What you are building here is needlessly javascript dependent.

Richard.
Feb 19 '06 #2
Thanks a lot Richard. I appreciate your help. Next time I will try to
be more specific when I post something here.

Feb 19 '06 #3

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

Similar topics

3
by: PW | last post by:
I use a multi-select menu in an ASP so the user can pick several values. <select multiple name="lbErrorType" size="12"> However, after I have saved the records to the DB, and the user comes...
18
by: booner | last post by:
I have a form that when it loads I would like to highlight the values (from a DB) that have been selected in a multiple selection list (<select multiple="true">. function onLoad() {...
7
by: Felix Natter | last post by:
hi, I have a php-Script which gets passed a Boat-ID and this is used to mark an element in a <select> as the default: <select name="boote" multiple="multiple" size="5"> <option...
5
by: Mark | last post by:
Hi - is it possible to pass a comma delimeted list to a javascript function, which will then loop through the list, and check against a listboxs items, and if the value in the list, corresponds to...
6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
92
by: bonneylake | last post by:
Hey Everyone, Well i was hoping someone could explain the best way i could go about this. i have a few ideas on how i could go about this but i am just not sure if it would work. Right now i...
58
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
6
by: phpnewbie26 | last post by:
My current form has one multiple select drop down menu as well as few other drop down menus that are single select. Originally I had it so that the multiple select menu was first, but this created...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.