Reply
creating a URL from dropdown menus
Old 05-06-2008, 12:57 AM creating a URL from dropdown menus
Junior Talker

Posts: 2
Name: Ron
I have what is probably very simple for experienced Javascript users, but I'm not one of them. I'm working on an intranet site, trying to create a command that will open a web page, using a specific URL made up from input from 3 drop down menus in html code. I'd like the new URL to be able to use the variables from the drop down menus, but can't seem to get this concept. I'm very, very new to Javascript. Someone who knows Javascript could probably figure it out in a few minutes. I'd appreciate someone taking a look at this. Here's some example code that I'm trying to make work.

<script language="javascript">

function Rpt8027URL()
{

var Rpt8027URL = "http://xyz.com/ncvo8027/2008-" +Rpt8027Month+ "-" +Rpt8027Day+ "." +Rpt8027Area+ ".ncvd8027.html";
return Rpt8027URL;

}
</script></head>
<body>
<p style="margin-top: 0; margin-bottom: 0">
<img src="logo.gif" width="600" height="60">
</p>
<p style="margin-top: 0; margin-bottom: 0"><font size="5" face="Comic Sans MS"><b>&nbsp;&nbsp;&nbsp;</b></font></p>
<table border=
"1" width="643">
<tr>
<td width="633">
<p style="margin-top: 0; margin-bottom: 0"><font size="5" face="Comic Sans MS"><b>National
Standards Carry-Over Reports:
</b></font></p>
<hr>
<p style="margin-top: 0; margin-bottom: 0"><font size="5" face="Comic Sans MS"><b>NCVO8027:</b></font></p>
<p style="margin-top: 0; margin-bottom: 0">Select Date, Geographic area and then
click &quot;RUN&quot;&nbsp;&nbsp;
</p>
<table border="1" width="76%">
<tr>
<td width="55%">
<p style="margin-top: 0; margin-bottom: 0"><b>Select a Date:&nbsp;</b></p>
<p style="margin-top: 0; margin-bottom: 0">&nbsp;<select size="1" name="Rpt8027Month">
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;

<select size="1" name="Rpt8027Day">
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;</p>
</td>
<td width="45%">
<B>Select Georgraphic Area:</B><br>
<SELECT NAME="Rpt8027Area" size="1"><OPTION VALUE="CA" NAME="Rpt8027Area">CA50</option>
<OPTION VALUE="TX" NAME="Rpt8027Area">TX88</option>
<OPTION VALUE="NY" NAME="Rpt8027Area">NY05</option>
</SELECT></td>
</tr>
</table>
<FORM><INPUT type="button" value="Run Report" onClick="Rpt8027URL();"></FORM></body>


Please help. Thank you. :-)
VZRon is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 05-07-2008, 12:58 AM Re: creating a URL from dropdown menus
Average Talker

Posts: 18
Name: TK
Hi VZRon,

One thing to keep in mind is that Javascript isn't immediately aware of any HTML element, even if you give that element a NAME or ID attribute. So in the Rpt8027URL function, the string that you're concatenating is not made up of the values of your <select> elements, but rather it's made up of Javascript variables that have the same name that you're declaring on the fly.

Here's one way that you can get the values of the <select> elements into Javascript variables.

Code:
function Rpt8027URL {
  var rptDay = document.getElementsByName("Rpt8027Day")[0].value;
  var rptMonth = document.getElementsByName("Rpt8027Month")[0].value;
  var rptArea = document.getElementsByName("Rpt8027Area")[0].value;
  var Rpt8027URL = "http://xyz.com/ncvo8027/2008-" +rptMonth+ "-" +rptDay+ "." +rptArea+ ".ncvd8027.html";
  alert ("You are going to be redirected to " + Rpt8027URL);
  window.location(Rpt8027URL);
}
In this code, we declared the variables rptDay, rptMonth, and rptArea. The document.getElementsByName function returns an array of HTML elements with a specific NAME attribute. The [0] that comes after it means we want to work with the 0th element (the very first element in that array) and the .value retrieves the value property of that element.

More often, you'll see Javascript code that uses the document.getElementById function. This returns a single HTML element that has an ID attribute set. It saves you from have to using square brackets to specify a particular element. In your HTML, you'd use <select size="1" id="Rpt8027Month">

Finally notice the window.location() function at the end. This is simply redirects the user to the new URL. You don't need to return any value because the submit button's "onclick" handler wouldn't do anything with it anyway. Since we're using window.location to do the redirection instead <form action="...">, you don't actually need the FORM element at all in your HTML.
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Old 05-07-2008, 01:37 PM Re: creating a URL from dropdown menus
Junior Talker

Posts: 2
Name: Ron
Thank you, zxcvbnm60! Your code did the trick. It was a well written/coded fix that explained the problem and the answer. Thanks so much.
VZRon is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to creating a URL from dropdown menus
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15464 seconds with 13 queries