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.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help in this!!

    hi, i using ajax to help to verify user, n using a <span> to show Available or unavailable, the <span> able to show Available or unAvailable but i can't be able to get the value, it detect the span has no value.


    HTML code
    ----------
    Code:
    <script src="functions.js"></script>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <form action="p/pRegisterClient.jsp" name="register">
                LoginID: <input type="text" id="un">
                <input type="button" value="Check" onclick="validLogin()">
                <span id="outputArea"></span>
     <input type="submit" value="Register" name="submitBtn" disabled>
            </form>
        </body>
    </html>

    javascript code
    ---------------
    Code:
    function ajax(str,outArea,link)
    {
    
        if (str=="")
        {
          document.getElementById(outArea).innerHTML="";
          return;
        }
        if (window.XMLHttpRequest)
          xmlhttp=new XMLHttpRequest();// code for IE7+, Firefox, Chrome, Opera, Safari
        else
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");// code for IE6, IE5
    
        xmlhttp.onreadystatechange=function()
        {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            document.getElementById(outArea).innerHTML=xmlhttp.responseText;
        }
    
        xmlhttp.open("GET",link+str,true);
        xmlhttp.send();
        
    }
    
    function validLogin()
    {
    
        var str=document.getElementById("un").value;
        
        ajax(str,'outputArea','p/pAjax.jsp?un=');
    
        output=document.getElementById('outputArea').innerHTML;
        output=output.substr(0, 10).trim();//due to bug in firefox
    
        alert(output);
    
        if (output=="Available")
            document.register.submitBtn.disabled=false;
        else
            document.register.submitBtn.disabled=true;
    }
    Last edited by xporiten; 12-28-2010 at 05:12 PM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    The first A in AJAX means: Asynchronous

    Which means: Execute the request in the background, continue executing the javascript in the meantime and automatically call the callback function (onreadystatechange) as soon as the requests changes state.

    So the AJAX request is being started, but javascript continues with determining the value (innerHTML) of the span which has not been filled yet with the AJAX response.

    Solution: You can determine the innerHTML of the span after it has been filled ... which would be best inside the onreadystatechange function

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks... ^_^


  •  

    Posting Permissions

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