Solved

Need to return a variable to a function

Posted on 2012-03-20
2
209 Views
Last Modified: 2012-06-27
Hello,

I am having an AJAX/ Javascript problem.  This is how my script is supposed to work.  A user selects one of two options in a drop down box.   If option 1 is selected the url that gets sent is findSchoolType1.php.  If option two is selected  the url is findSchoolType2.php.  The code only works when I uncomment the STRurl = "findSchoolType1.php" that isn't part of the if/else statement.  Is the problem with the if/else?

function selectSchoolType() {		
		var selected = document.getElementById('displaySchoolOptions')
		
		selected.onchange = function() {
		if (selected.value == 1) 
		  {
			  alert("value is 1")
			return strURL="findSchoolType1.php?";
			  }
		else { 
		alert("value is 2")
		return strURL="findSchoolType2.php?";
		}
		
		}
		 //strURL="findSchoolType1.php?";
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {

Open in new window

0
Comment
Question by:NewWebDesigner
2 Comments
 
LVL 13

Accepted Solution

by:
ansudhindra earned 500 total points
Comment Utility
declare "strURL" as global variable or at least inside "selectSchoolType()" function.
0
 
LVL 7

Expert Comment

by:micropc1
Comment Utility
It is a problem with scope - because you are not declaring strURL, it is assumed to be declared inside the if block, so it is not visible to the code when the if block terminates. If you want to access strURL outside the if block you need to do as ansudhindra mentioned and declare it as a global variable outside the method, which will make it available to all of your code - or directly inside the selectSchoolType() method, which will make it available to all of the code inside that method. Just add "var strURL;" wherever you want to declare it.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
resizeing PHP image 2 21
Re-position the objects 7 46
Instering to MySQL table 5 15
onOpen 14 38
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now