Solved

Need to return a variable to a function

Posted on 2012-03-20
2
211 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
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 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)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

778 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