cannot view database on HTML form using AJAX
[nobr]Hi .. ive been trying out AJAX and i wrote an html form which just displays the contents of a table in a MYSQL database. The problem is, NOTHING is happening.. No error messages, no alerts (inspite of putting so many of them) nothing. I m kinda lost as to whether and connection has been made successfully, whether responseText is retrieving data or not..
heres my code.. Help will be appreciated :)
<html>
<body bgcolor="#99FF66">
<script language="javascript" type="text/javascript">
var httpreq=false;
function showTable()
{
httpreq=initiateRequest();
if(httpreq==false)
{
alert("Your browser does not support AJAX !!!");
return;
}
else
{
var url="/opt/sun/domains/domain1/docroot/get_details.jsp";//i provided the full path
var temp = httpreq.readyState;
alert("Your server state is " + temp);
if(httpreq.readyState==4 || httpreq.readyState==3)//somewhr i read i need to handle readyState so thats why i put this
{
httpreq.onreadystatechange = getcontents;
}
httpreq.open('GET',url,false);
httpreq.send(null);
}
}
function initiateRequest()
{
try
{//for Internet Explorer
httpreq =new ActiveXObject("Msxml12.XMLHTTP");
}
catch(Exception e)
{
try
{
httpreq =new ActiveXObject("Microsoft.XMLHTTP");
}
catch(Exception e1)
{
httpreq=false;
}
}
if(!httpreq && typeof XMLHttpRequest !='undefined')
{
//for firefox...
httpreq =new XMLHttpRequest();
}
elseif(!httreq)
{
alert('Error: Cannot make XMLHTTP instance');
returnfalse;
}
return httpreq;
}
function getcontents()
{
if(httpreq.readyState==4)//state 4 means server is ready for requests.
{
alert("Connected to server and its ready for requests");
if(httpreq.status==200)//everything is OK
{
var response = httpreq.responseText;
document.getElementById('myDiv').innerHTML=response;
}
elseif(httpreq.status == 404)
{
alert("SORRY URL DOES NOT EXIST!");
}
else
{
alert("ERROR : The satus code is" + httpreq.status);
}
}
}
<form name="myform">
<br/><br/>
<input type="button" onclick="showTable()" value="DONE"/>
<br/><br/>
</form>
<div id='myDiv'>The contents of the table are</div>
As u can see a simple form wid a button to display.Will be adding more to the form once this works.
The jsp page is running absolutely fine as i executed it separately and it was retrieving the table perfectly.. kindly tell me if any changes have to be made here..[/nobr]

