473,699 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="whateve r.php" method="post">
<select name="zip_code"
onchange="makeR equest('getCity .php?state='+th is.form.zip_cod e.options[this.form.zip_c ode.selectedInd ex].value)"
multiple="multi ple" size="20">
<?
while($row = mysql_fetch_arr ay($rs)){
?><option
value="<?=$row['f_fips_code']?>"><?=$row['f_name']?></option><?
}
?>
</select>
<select name="test" id="t1" multiple="multi ple" size="40">
<option>Selec t 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 3656
ar*********@gma il.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="whateve r.php" method="post">
<select name="zip_code"
onchange="makeR equest('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_c ode.selectedInd ex].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_c ode - 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="multi ple" 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_arr ay($rs)){
?><option
value="<?=$row['f_fips_code']?>"><?=$row['f_name']?></option><?
}
?>
</select>
<select name="test" id="t1" multiple="multi ple" size="40">
<option>Selec t 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
2411
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 back to edit a prior record, I re-populate the multiple select box and do a "<option selected>" for each record that is saved to the DB. <option selected>
18
5715
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() { document.forms.elements.value = "<value from DB>"; }
7
7897
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 value='43'>Aisha</option> <option value='40'>Amsterdam</option> <option value='55'>Asmaa</option> <option value='20'>Beau Rivage</option> <option value='48'>Blue Perl</option>
5
2338
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 an item in the listbox, the listbox item should be 'selected' - I'd like this to happen for a multiple select list box. Something along the lines of (sorry, I'm not knowledgable in JS just yet, but need to solve the problem): function...
6
4294
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. Based on the number of variables passed through a GET string, I want to multiply the total number of selected items for each together to see how many possible combinations the selected items are generating. The following snippet of code...
92
4730
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 have a form where you can add and delete multiple serial information. This works wonderful. But now for every serial information i add i need to be able to add and remove multiple parts to that serial. heres an example of what i mean serial...
58
8087
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 the parts section an i seem to be having trouble. When i try to insert into the parts section i get the error Invalid character value for cast specification. But not sure what i am doing wrong. Here is what i am using to insert. All the sections...
482
27746
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. what i am trying to display previously entered multiple fields. I am able to get my serial fields to display correctly, but i can not display my parts fields correctly. Currently this is what it does serial information 1 parts 1
6
7566
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 the problem that when I went to select the other drop down menus, the selections i made on the multiple select one would clear. Then I had tried putting the multiple select menu last so that the selections wouldn't clear but then after clicking the...
0
9187
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
9053
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
8936
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
7776
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...
1
6540
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
5879
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
4636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
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
2
2360
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.