Solved

Need to return a variable to a function

Posted on 2012-03-20
2
210 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
ID: 37745553
declare "strURL" as global variable or at least inside "selectSchoolType()" function.
0
 
LVL 7

Expert Comment

by:micropc1
ID: 37745593
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP warning 4 30
what is the difference between a object and a array 4 26
Sorting a string 1 13
bootstrap footer centering and columns alignement 3 23
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

895 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

17 Experts available now in Live!

Get 1:1 Help Now