...

View Full Version : Resolved Need help in this!!



xporiten
12-28-2010, 06:04 AM
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
----------

<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
---------------

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;
}

devnull69
12-28-2010, 07:21 AM
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

xporiten
12-28-2010, 04:11 PM
thanks... ^_^



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum