AJAX Basic Example
[CODE=javascript]
<script>
//set the variables.
function getArticle(id) {
var link = "/getNews.asp";
xmlhttpPost(lin k, id);
}
//Make the actual connection.
function xmlhttpPost(str URL, id) {
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttp Request) {
self.xmlHttpReq = new XMLHttpRequest( );
}
// IE
else if (window.ActiveX Object) {
self.xmlHttpReq = new ActiveXObject(" Microsoft.XMLHT TP");
}
self.xmlHttpReq .open('POST', strURL, true);
self.xmlHttpReq .setRequestHead er('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq .onreadystatech ange = function() {
if (self.xmlHttpRe q.readyState == 4) { //ready state 4 means its complete.
updatepage(self .xmlHttpReq.res ponseText);
}
}
self.xmlHttpReq .send(getquerys tring(id));
}
//set the query string to be sent
function getquerystring( id) {
qstr = 'data=' + escape(id); // NOTE: no '?' before querystring
return qstr;
}
//put the data on the page.
function updatepage(str) {
document.getEle mentById("data" ).innerHTML = str;
}
</script>
<select onchange="getAr ticle(this.valu e);">
<option value="1">Train hits car</option>
<option value="2">Airpl ane Crashes</option>
<option value="3">Menta l Health</option>
</select>
<div id="data"></div>
[/CODE]
Now for the getNews.asp --- not fully functional code but an example
[CODE=ASP]
<%
news_id = request("id")
strSQL = "select * from news where news_id = " & new_id
rs.open strSQL, connection, 1, 1
if not rs.eof then
response.write rs("story")
end if
%>
[/CODE]
[CODE=javascript]
<script>
//set the variables.
function getArticle(id) {
var link = "/getNews.asp";
xmlhttpPost(lin k, id);
}
//Make the actual connection.
function xmlhttpPost(str URL, id) {
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttp Request) {
self.xmlHttpReq = new XMLHttpRequest( );
}
// IE
else if (window.ActiveX Object) {
self.xmlHttpReq = new ActiveXObject(" Microsoft.XMLHT TP");
}
self.xmlHttpReq .open('POST', strURL, true);
self.xmlHttpReq .setRequestHead er('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq .onreadystatech ange = function() {
if (self.xmlHttpRe q.readyState == 4) { //ready state 4 means its complete.
updatepage(self .xmlHttpReq.res ponseText);
}
}
self.xmlHttpReq .send(getquerys tring(id));
}
//set the query string to be sent
function getquerystring( id) {
qstr = 'data=' + escape(id); // NOTE: no '?' before querystring
return qstr;
}
//put the data on the page.
function updatepage(str) {
document.getEle mentById("data" ).innerHTML = str;
}
</script>
<select onchange="getAr ticle(this.valu e);">
<option value="1">Train hits car</option>
<option value="2">Airpl ane Crashes</option>
<option value="3">Menta l Health</option>
</select>
<div id="data"></div>
[/CODE]
Now for the getNews.asp --- not fully functional code but an example
[CODE=ASP]
<%
news_id = request("id")
strSQL = "select * from news where news_id = " & new_id
rs.open strSQL, connection, 1, 1
if not rs.eof then
response.write rs("story")
end if
%>
[/CODE]
Comment