Solved

Need to return a variable to a function

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

738 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