Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need to return a variable to a function

Posted on 2012-03-20
2
Medium Priority
?
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
ansudhindra earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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.
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)

670 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