title: Javascript Returing Value from httprequest
body: <p>The php script is returning a value and the 1st alert works.</p>
  
  <p>I am unable to reference the value returned by httprequest at the 2nd alert.  Ideally, I would call the function get_captcha() - and it would return the value - its just that I dont know how to do this.</p>
  
  <p>I realize setting the variable globally may not be the best way to do this but its the only thing I could think of - Im open to alternatives. </p>
  
  <pre><code>  &lt;script type="text/javascript"&gt;
     var url = "captcha_get_code.php"; // The server-side script 
     var cap;
  
     function ValidateForm() {
      get_captcha()
      alert(cap); //undefined
  
      }
  
      function get_captcha() {      
        http.open("GET", url, true); 
         http.onreadystatechange = handleHttpResponse; 
         http.send(null); 
  
  }
  
    function handleHttpResponse() {    
      if (http.readyState == 4) { 
         if (http.status==200) { 
              //return http.responseText; 
          cap=http.responseText; 
              alert(cap);  //this one works
              }
  
          } 
      } 
  
  
  
  function getHTTPObject() { 
    var xmlhttp; 
  
    if(window.XMLHttpRequest){ 
      xmlhttp = new XMLHttpRequest(); 
    } 
    else if (window.ActiveXObject){ 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      if (!xmlhttp){ 
          xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
      } 
  
  } 
    return xmlhttp;   
  } 
  var http = getHTTPObject(); // We create the HTTP Object 
  
  
  
  
  
  &lt;/script&gt;   
  </code></pre>