Mithun Ds
Mithun Ds

Reputation: 131

How to access javascript variable value in ajax code

I am trying to access the javascript variable value from this code to ajax code

I tried to access like this but it is not working-

javascript code

<script type="text/javascript">

function getResults() 
{
    var radios = document.getElementsByName("address");    
    for (var i = 0; i < radios.length; i++) 
    {       
        if (radios[i].checked) 
        {
       var a = radios[i].value
            break;
        }
    }
}


function remove(a) 
{
     alert(a);
     if(a=="") 
     {
         document.getElementById("txtHint").innerHTML="";
         return;
     }

     if(window.XMLHttpRequest) 
     {
         xmlhttp=new XMLHttpRequest();
     } 
     else 
     {
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     xmlhttp.onreadystatechange = function() 
     {
         if(xmlhttp.readyState==4 && xmlhttp.status==200) 
         {
              document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
         }
     }
     xmlhttp.open("GET","theaterdel.php?q="+a,true);
     xmlhttp.send();
}

</script>

In this code I am trying to access the variable a's value from getResults() function to remove() like this

remove(a)
{}

But it is not working, when I alert a's value inside remove(a){}, it is printing undefined

Upvotes: 3

Views: 1770

Answers (5)

RunningAdithya
RunningAdithya

Reputation: 1754

You have declared variable "a" as local to getresults function. Declare variable "a" as global, don't use var keyword.

if (radios[i].checked) 
    {
     a = radios[i].value
        break;
    }

You are having two different functions and to get variable of one in another function, declare it globally.

Upvotes: 3

Sonu Sindhu
Sonu Sindhu

Reputation: 1792

try this

function getResults() 
{
    var radios = document.getElementsByName("address");    
    for (var i = 0; i < radios.length; i++) 
    {       
        if (radios[i].checked) 
        {
            var a = radios[i].value
            remove(a);
            break;
        }
    }
}

call a function inside

hope it will help.

Upvotes: 4

James
James

Reputation: 13501

You've defined a inside the getResults() function using the 'var' keyword. This means it will only be in scope inside that function.

If you want to use that variable in another function, you'll need to pass it across when you call the other function - simple example is below:

function myFunction() {
 var a = "this is a";
 secondFunction(a);
}

function secondFunction(a) {
 alert(a);
}

An alternative is to declare the variable so it has global scope - but that's not a great idea unless you're confident that it won't conflict with any other global variables that might exist on your page.

You're better off constraining scope unless you absolutely need to have global variables. It'll make your code neater, and debugging easier.

Upvotes: 2

hex4
hex4

Reputation: 695

you need to declare the variable outside the function like

var a = null
function getResults()

when you declare it outside the function you can access the variable from anywhere you want

also, look into jQuery .ajax() for simpler code regarding AJAX

Upvotes: 2

Sahil Mittal
Sahil Mittal

Reputation: 20753

Globally define the variable -

var a= "hi";

function getResults(){ ... }

function remove() { alert(a); ... }

Upvotes: 4

Related Questions