Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    responseText returns correct value on ALERT() but...

    RESOLVED

    Thank you!


    Greetings all!

    Im looking for some help regarding responseText, so please have a look im so mad right now because i have been struggeling for hours

    XMLHttpRequestObject.responseText returns correct value when i do alert(XMLHttpRequestObject.responseText); see line

    PHP Code:
    var fnWhenDone = function (XMLHttpRequestObject) { alert(XMLHttpRequestObject.responseText); }; 
    But problem is that i want to save down the response to a variable... so i try to change it into

    PHP Code:
    var fnWhenDone = function (XMLHttpRequestObject) { varTest XMLHttpRequestObject.responseText; }; 
    When i try to alert varTest later i get "Undifined"... im pretty new to javascript and have been stuck for hours ...


    See full code below

    PHP Code:

    var myConn = new XHConn();

    if (!
    myConn) { alert("XMLHTTP not available. Try a newer/better browser."); }

    var 
    fnWhenDone = function (XMLHttpRequestObject) { alert(XMLHttpRequestObject.responseText); };

    myConn.connect("validateSearch.php""POST""foo=bar&baz=qux"fnWhenDone);


    function 
    XHConn()
    {
      var 
    xmlhttpbComplete false;
      try { 
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (
    e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (
    e) { try { xmlhttp = new XMLHttpRequest(); }
      catch (
    e) { xmlhttp false; }}}
      if (!
    xmlhttp) return null;
      
    this.connect = function(sURLsMethodsVarsfnDone)
      {
        if (!
    xmlhttp) return false;
        
    bComplete false;
        
    sMethod sMethod.toUpperCase();
        try {
          if (
    sMethod == "GET")
          {
            
    xmlhttp.open(sMethodsURL+"?"+sVarstrue);
            
    sVars "";
          }
          else
          {
            
    xmlhttp.open(sMethodsURLtrue);
            
    xmlhttp.setRequestHeader("Method""POST "+sURL+" HTTP/1.1");
            
    xmlhttp.setRequestHeader("Content-Type""application/x-www-form-urlencoded");
          }
          
    xmlhttp.onreadystatechange = function(){
            if (
    xmlhttp.readyState == && !bComplete)
            {
              
    bComplete true;
              
    fnDone(xmlhttp);
            }};
          
    xmlhttp.send(sVars);
        }
        catch(
    z) { return false; }
        return 
    true;
      };
      return 
    this;

    Last edited by Jawny; 09-02-2010 at 06:19 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    You just have to *declare* that variable at page scope!!!

    That is:
    Code:
    <script>
    var varTest;
    ...
    ... later in the code ...
        var fnWhenDone = function (XMLHttpRequestObject) { 
            varTest = XMLHttpRequestObject.responseText; };  
    ...
    </script>
    Or some equivalent way. The point is that varTest has to exist outside of the fnWhenDone function if you then want to *see* it outside the function.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by Jawny View Post
    Greetings all!

    Im looking for some help regarding responseText, so please have a look im so mad right now because i have been struggeling for hours

    XMLHttpRequestObject.responseText returns correct value when i do alert(XMLHttpRequestObject.responseText); see line

    PHP Code:
    var fnWhenDone = function (XMLHttpRequestObject) { alert(XMLHttpRequestObject.responseText); }; 
    But problem is that i want to save down the response to a variable... so i try to change it into

    PHP Code:
    var fnWhenDone = function (XMLHttpRequestObject) { varTest XMLHttpRequestObject.responseText; }; 
    When i try to alert varTest later i get "Undifined"... im pretty new to javascript and have been stuck for hours ...


    See full code below

    PHP Code:

    var myConn = new XHConn();

    if (!
    myConn) { alert("XMLHTTP not available. Try a newer/better browser."); }

    var 
    fnWhenDone = function (XMLHttpRequestObject) { alert(XMLHttpRequestObject.responseText); };

    myConn.connect("validateSearch.php""POST""foo=bar&baz=qux"fnWhenDone);


    function 
    XHConn()
    {
      var 
    xmlhttpbComplete false;
      try { 
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (
    e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (
    e) { try { xmlhttp = new XMLHttpRequest(); }
      catch (
    e) { xmlhttp false; }}}
      if (!
    xmlhttp) return null;
      
    this.connect = function(sURLsMethodsVarsfnDone)
      {
        if (!
    xmlhttp) return false;
        
    bComplete false;
        
    sMethod sMethod.toUpperCase();
        try {
          if (
    sMethod == "GET")
          {
            
    xmlhttp.open(sMethodsURL+"?"+sVarstrue);
            
    sVars "";
          }
          else
          {
            
    xmlhttp.open(sMethodsURLtrue);
            
    xmlhttp.setRequestHeader("Method""POST "+sURL+" HTTP/1.1");
            
    xmlhttp.setRequestHeader("Content-Type""application/x-www-form-urlencoded");
          }
          
    xmlhttp.onreadystatechange = function(){
            if (
    xmlhttp.readyState == && !bComplete)
            {
              
    bComplete true;
              
    fnDone(xmlhttp);
            }};
          
    xmlhttp.send(sVars);
        }
        catch(
    z) { return false; }
        return 
    true;
      };
      return 
    this;

    var fnWhenDone = function (XMLHttpRequestObject......

    fnDone(xmlhttp);

    Is this a typo or what ?

    anyways...

    fnDone(xmlhttp.responseText);

    var fnWhenDone = function (text) { varTest = text; };

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    I would suggest that you are trying to find the xmlhttp object in the wrong order. Current versions of MSIE support the standard, so go for it first:
    Code:
      try { xmlhttp = new XMLHttpRequest(); }
      catch (e) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e) ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey guys thank you for trying to help me! i really appreciate it!

    I took your advice i tryed but still not luck i think its really odd and still getting undifined.

    I got it to work if i declared the variable validateResponse outside the function function get(obj) but but then i ended up getting undifined in the first click but correct second click and next.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <
    html>
        <
    head>
            <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
            <
    meta http-equiv="Content-Language" content="sv">
            <
    title>test</title>
        </
    head>
        <
    body>
        
            <
    script type="text/javascript">
            
            function 
    get(obj)
            {
                var 
    validateResponse;
                
                var 
    myConn = new XHConn();
        
                if (!
    myConn) { alert("XMLHTTP not available. Try a newer/better browser."); }
                
                var 
    fnWhenDone = function (XMLHttpRequestObject) { validateResponse XMLHttpRequestObject.responseText; };
                
                
    myConn.connect("validateSearch.php""POST""foo=bar&baz=qux"fnWhenDone);
                
                
    alert(validateResponse);
            }
        
        
            function 
    XHConn()
            {
              var 
    xmlhttpbComplete false;
              try { 
    xmlhttp = new XMLHttpRequest(); }
              catch (
    e) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
              catch (
    e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
              catch (
    e) { xmlhttp false; }}}
              if (!
    xmlhttp) return null;
              
    this.connect = function(sURLsMethodsVarsfnDone)
              {
                if (!
    xmlhttp) return false;
                
    bComplete false;
                
    sMethod sMethod.toUpperCase();
                try {
                  if (
    sMethod == "GET")
                  {
                    
    xmlhttp.open(sMethodsURL+"?"+sVarstrue);
                    
    sVars "";
                  }
                  else
                  {
                    
    xmlhttp.open(sMethodsURLtrue);
                    
    xmlhttp.setRequestHeader("Method""POST "+sURL+" HTTP/1.1");
                    
    xmlhttp.setRequestHeader("Content-Type""application/x-www-form-urlencoded");
                  }
                  
    xmlhttp.onreadystatechange = function(){
                    if (
    xmlhttp.readyState == && !bComplete)
                    {
                      
    bComplete true;
                      
    fnDone(xmlhttp);
                    }};
                  
    xmlhttp.send(sVars);
                }
                catch(
    z) { return false; }
                return 
    true;
              };
              return 
    this;
            }
            
    </script>


            <form action="javascript:get(document.getElementById('myform'));" method="post" autocomplete="off" name="myform" id="myform">
            <input type="submit" value="Search" name="submit">
            </form>

        </body>
    </html> 
    validateSearch.php
    PHP Code:
    <?php
    echo "works";
    ?>
    Last edited by Jawny; 09-02-2010 at 11:23 AM.

  • #6
    Regular Coder
    Join Date
    Nov 2009
    Posts
    247
    Thanks
    4
    Thanked 22 Times in 22 Posts
    Jawny:

    Here's a live example of AJAX Post, with responseText:

    http://www.javascript-demos.com/1/AJAX_Post.html

    This is the .php file associated with that demo:

    PHP Code:
    <?php 
        $message 
    "First Name - " $_POST['fname'] . "<br>";
        
    $message .= "Surname - " $_POST['surname']; 
        echo 
    stripslashes($message);
    ?>

  • #7
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Sciliano View Post
    Jawny:

    Here's a live example of AJAX Post, with responseText:

    http://www.javascript-demos.com/1/AJAX_Post.html

    This is the .php file associated with that demo:

    PHP Code:
    <?php 
        $message 
    "First Name - " $_POST['fname'] . "<br>";
        
    $message .= "Surname - " $_POST['surname']; 
        echo 
    stripslashes($message);
    ?>
    I had a look on that code and i still dont understand why mine doesnt work

  • #8
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay i will pay $20 (PayPal) to the one who fixes my problems and posts it first.

    My work has stalled and i really need to keep going

    Best Regards
    Jawn

  • #9
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by Jawny View Post
    Okay i will pay $20 (PayPal) to the one who fixes my problems and posts it first.

    My work has stalled and i really need to keep going

    Best Regards
    Jawn
    http://javascript.daveyerwin.com/
    Last edited by DaveyErwin; 09-03-2010 at 10:49 AM. Reason: url change

  • #10
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DaveyErwin View Post
    Took your code and changed it into how i wanted but problem persists...

    First click i get "undifined" second and next one send me the correct value.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
        <
    title></title>
    </
    head>
    <
    body>
    Hiyas click the button !
    <
    input type="button" onclick="doit()"/>
    </
    body>
    </
    html>
    <
    script
        var 
    myConn = new XHConn();
         var 
    response;
         
        if (!
    myConn) { alert("XMLHTTP not available. Try a newer/better browser."); }
     
        var 
    fnWhenDone = function (XMLHttpRequestObject) { response XMLHttpRequestObject.responseText; };
        
    doit = function () {
            
    myConn.connect("validateSearch.php""GET""foo=bar&baz=qux"fnWhenDone);
            
            if(
    response == "works")
            {
                
    alert('Searching...');
            }
            else
            {
                
    // Show error
                
    alert(response);
            }
        }
     
         
     
        function 
    XHConn() {
            var 
    xmlhttpbComplete false;
            try { 
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
            catch (
    e) {
                try { 
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
                catch (
    e) {
                    try { 
    xmlhttp = new XMLHttpRequest(); }
                    catch (
    e) { xmlhttp false; } 
                } 
            }
            if (!
    xmlhttp) return null;
            
    this.connect = function (sURLsMethodsVarsfnDone) {
                if (!
    xmlhttp) return false;
                
    bComplete false;
                
    sMethod sMethod.toUpperCase();
                try {
                    if (
    sMethod == "GET") {
                        
    xmlhttp.open(sMethodsURL "?" sVarstrue);
                        
    sVars "";
                    }
                    else {
                        
    xmlhttp.open(sMethodsURLtrue);
                        
    xmlhttp.setRequestHeader("Method""POST " sURL " HTTP/1.1");
                        
    xmlhttp.setRequestHeader("Content-Type""application/x-www-form-urlencoded");
                    }
                    
    xmlhttp.onreadystatechange = function () {
                        if (
    xmlhttp.readyState == && !bComplete) {
                            
    bComplete true;
                            
    fnDone(xmlhttp);
                        } 
                    };
                    
    xmlhttp.send(sVars);
                }
                catch (
    z) { return false; }
                return 
    true;
            };
            return 
    this;
        }  
     
     
    </script> 

  • #11
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts

    Question

    Quote Originally Posted by Jawny View Post
    Took your code and changed it into how i wanted but problem persists...

    First click i get "undifined" second and next one send me the correct value.
    You don't have that problem with my site.
    Do you ?

  • #12
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DaveyErwin View Post
    You don't have that problem with my site.
    Do you ?
    Works fine till i change Alert to declaring it to a variable.

  • #13
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by Jawny View Post
    Works fine till i change Alert to declaring it to a variable.
    Take a look now and see what you think.

  • #14
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DaveyErwin View Post
    Take a look now and see what you think.
    Awesome works, pm me your PayPal

  • #15
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by Jawny View Post
    Awesome works, pm me your PayPal
    Take the kids out to the icecream vender and tell em
    Uncle daves buyin.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •