AJAX is actually very easy. You can pass your fields via querystring if you like, and return the server-side data via XML. Since XML is hierarchical, you can have as much data as you like, and parse through the results with childNodes[0].nodeValue. Here is a simple AJAX example that you can expand upon.
One quick note: The Javascript code has a line that reads:
var myurl = 'http://localhost/ajax/clock.aspx';
You will need to change this URL to wherever the server side code (clock.aspx) exists
You are welecome to modify this code in any way you like.
time_demo.html : Client front end
Code:
<html>
<head>
<title>Very Simple AJAX Example That Returns Time</title>
<style>
.displaybox {
width:150px;
background-color:#ffffff;
border:2px solid #000000;
padding:10px;
font:24px normal verdana, helvetica, arial, sans-serif;
}
</style>
<script language="JavaScript" src="ajax.js">
</script>
</head>
<body style="background-color:#cccccc" onLoad="getServerTime()">
<center>
<p style="font-family:arial;font-size:28;">Very Simple AJAX Example That Returns Time<p>
<form>
<input type="button" value="Get Server Time" onClick="getServerTime()">
</form>
<div id="showtime" class="displaybox" style="width: 358; height: 20"></div>
</center>
</body>
</html>
Ajax.js : Javascript
Code:
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
return req;
}
var http = getXMLHTTPRequest();
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function getServerTime() {
var myurl = 'http://localhost/ajax/clock.aspx';
// MyRand generates a random number to avoid browser cache problem
myRand = parseInt(Math.random()*999999999999999);
var modurl = myurl+"?rand="+myRand;
http.open("GET", modurl, true);
http.onreadystatechange = useHttpResponse;
http.send(null);
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function useHttpResponse() {
if (http.readyState == 4) {
if(http.status == 200) {
var timeValue = http.responseXML.getElementsByTagName("timenow")[0];
document.getElementById('showtime').innerHTML = ReplaceBadChars(timeValue.childNodes[0].nodeValue);
}
} else {
document.getElementById('showtime').innerHTML = '<img src="anim.gif">';
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function ReplaceBadChars(strTmp){
strTmp=strTmp.replace("&","&");
strTmp=strTmp.replace("<","<");
return strTmp;
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
clock.aspx : Server side code (ASP.NET in this example)
Code:
<%@ Page Language="VB" Debug="false"%>
<script runat="server">
' =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Sub Page_Load
If Not IsPostBack Then
End If
dim dtmDate as DateTime
dtmDate=DateTime.Now()
Dim varMsg as string
varMsg=dtmDate.ToString("g")
Dim strXmlNames as string=""
strXmlNames="<?xml version=""1.0"" encoding=""ISO-8859-1"" ?><clock1><timenow>" & ReplaceBadChars(varMsg) & "</timenow></clock1>"
Response.Clear()
Response.ContentType ="text/xml"
Response.Write(strXmlNames)
Response.End()
End Sub
' =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Function Replace(txtSource as string, ToBeReplaced as string, ReplacedWith as string ) as string
dim objStringBuilder as StringBuilder
objStringBuilder = New StringBuilder(txtSource)
objStringBuilder.Replace(ToBeReplaced,ReplacedWith)
Replace=objStringBuilder.ToString()
End function
' =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Function ReplaceBadChars(strTmp as string) as string
ReplaceBadChars=Replace(strTmp,"&","&")
ReplaceBadChars=Replace(ReplaceBadChars,"<","<")
End Function
' =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
</script>
Bookmarks