codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  How to post array values back to a calling page  jimfer at 21:47 on Thursday, September 04, 2003
 

I have a page that calls a pop-window with a Listview control enabled for multiple Selections. I don't know how many selections the user will make. I would like to pass this info back to the calling page. THanks!

  Re: How to post array values back to a calling page  Troy Wolf at 23:44 on Thursday, September 04, 2003
 

I am assuming that since you posted in the ASP forum, that by "Listview" you mean a multi <SELECT>.

Copy and paste this code into a new HTML file, then double-click it. Select 1 to 5 cars, then click "Submit". You will see a javascript alert that shows you which cars you selected.

<script language=javascript>
function ReturnCars()
{
var selectedCars;
var e = document.forms[0].cars;
for(i=0;i<e.length;i++)
{
if(e.selected)
{
if(!selectedCars)
{
selectedCars = e.text;
}
else
{
selectedCars += "," + e.text;
}
}
}
alert(selectedCars);
}
</script>
<form method=post action="/forum/MultiSelect.html">
<select name=cars multiple size=5>
<option>Ford</option>
<option>Chevy</option>
<option>Toyoya</option>
<option>Dodge</option>
<option>BMW</option>
</select>
<br>
<input type=button value="Submit" onclick=ReturnCars()>
</form>

Now all you have to do is instead of popping up an alert, send the comma-seperated list of cars back to a function on the parent page, then close the popup.

self.opener.SomeFunction(selectedCars);
self.close();


<Added>

I've done it again! I commonly use the variable "i" for an index. In javascript, you commonly place the index between square brackets. Well, in this CodeToad forum, an i between square brackets means italics! DOH! So my code example got corrupted. Below, I show the code again, this time using "idx" as my index variable.

<script language=javascript>
function ReturnCars()
{
var selectedCars;
var e = document.forms[0].cars;
for(idx=0;idx<e.length;idx++)
{
if(e[idx].selected)
{
if(!selectedCars)
{
selectedCars = e[idx].text;
}
else
{
selectedCars += "," + e[idx].text;
}
}
}
alert(selectedCars);
}
</script>
<form method=post action="/forum/MultiSelect.html">
<select name=cars multiple size=5>
<option>Ford</option>
<option>Chevy</option>
<option>Toyoya</option>
<option>Dodge</option>
<option>BMW</option>
</select>
<br>
<input type=button value="Submit" onclick=ReturnCars()>
</form>
Troy Wolf: site expert
SnippetEdit Website Editor









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  do static member are inherited
•  Re: Help with Using DataReader with StoredProcedure
•  Perl Script Output (w3c validator)
•  Re: HashMap question
•  focus management?
•  Looping Issue...Please help!
•  regarding hibernate
•  working with plugins
•  Checkbox Validation help.


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006