Solved

how to change it to AJAX

Posted on 2006-11-15
8
189 Views
Last Modified: 2010-04-06
I have a form with an iframe that refreshes every 5 seconds. I don't want it to refresh everytime but I need the latest data.

Basically this is for a chat program done in ASP. The form chatmain.asp contains an iframe on top and a textarea and submit button at the bottom of the form. When we type something and click the submit, the message gets added to a text file. The iframe display the page chatscreen.asp which reads the textfile and displays. The chatscreen.asp refreshes each 5 seconds. I will provide the code for the page that refreshes. I don't want this page to refresh, but should show the latest data.  I heard that it can be done using AJAX and that is what I want. I want somebody to change this code to AJAX.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim File_name
File_name = Request.QueryString("sFname")
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="refresh" content="5;URL=chatscreen.asp?sFname=<%=File_name%>" />
<title>Chat with Alpine Agent</title>
<style type="text/css">
<!--
.s1 {font-family: Arial; color:#333366; font-size:12px}

body {
      background-color: #FFFFFF;
}
-->
</style>

</head>

<body onload="self.location.href='#base'">
<p class="s1">
<%
dim objOpenFile, objFSO, strPath
strPath = Server.MapPath("./" & File_name & ".txt")
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
set objOpenFile = objFSO.OpenTextFile(strPath, 1)
While not objOpenFile.AtEndOfStream
      Response.Write objOpenFile.ReadLine
      Response.Write("<br />")
Wend
%>
<a id="bottom" name="base"><font color="#FFFFFF">Show this</font></a>
</p>
</body>
</html>
0
Comment
Question by:CWS (haripriya)
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
8 Comments
 
LVL 12

Expert Comment

by:netsmithcentral
ID: 17951058
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim File_name
File_name = Request.QueryString("sFname")
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chat with Alpine Agent</title>
<script type="text/javascript">
<!--
window.onload = updateChat;

function updateChat(){
 if (window.XMLHttpRequest){
  http_request = new XMLHttpRequest();
 }
 else if (window.ActiveXObject){
  try {
   http_request = new ActiveXObject('Msxml2.XMLHTTP');
  } catch (e) {      
   try {
    http_request = new ActiveXObject('Microsoft.XMLHTTP');
   } catch (e) {}
  }
 }
 http_request.onreadystatechange = function() {
  if(http_request.readyState == 4){
   if(http_request.status != 200){
    if(http_request.status == 403) alert('Chat log could not be updated.   403: Acess is forbidden');;
    else if(http_request.status == 404) alert('Chat log could not be updated.  404: No record of your chat exists.');
    else alert('Chat log could not be updated.  '+http_request.status+': Unknown error.');
   }
   else {
    try{http_request.setRequestHeader('content-type','text/html;charset=ISO_8859-1:1987');}catch(e){}
    setTimeout('updateChat();', 5000);
    document.getElementById('chatlog').innerHTML = http_request.responseText;
   }
  }
 };
 http_request.open('GET', <%=File_name&".txt"%>, true);
 http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 http_request.send(null);
}
//-->
</script>
<style type="text/css">
<!--
.s1 {font-family: Arial; color:#333366; font-size:12px}

body {
     background-color: #FFFFFF;
}
-->
</style>

</head>

<body onload="self.location.href='#base'">
<p class="s1" id="chatlog">
<%
dim objOpenFile, objFSO, strPath
strPath = Server.MapPath("./" & File_name & ".txt")
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
set objOpenFile = objFSO.OpenTextFile(strPath, 1)
While not objOpenFile.AtEndOfStream
      Response.Write objOpenFile.ReadLine
      Response.Write("<br />")
Wend
%>
</p>
<a id="bottom" name="base"><font color="#FFFFFF">Show this</font></a>
</body>
</html>
0
 
LVL 16

Author Comment

by:CWS (haripriya)
ID: 17953972
Hello netsmithcentral,

Thanks for your prompt reply. Unfortunately the page is not showing the latest data.
0
 
LVL 16

Author Comment

by:CWS (haripriya)
ID: 17954018
I want the page to check for the latest data every 5 seconds without refreshing the page.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:kelvinwkw
ID: 17954595
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim File_name
File_name = Request.QueryString("sFname")
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%randomize%>
<meta http-equiv="refresh" content="5;URL=chatscreen.asp?sFname=<%=File_name%>&dummy=<%=rnd*100%>" />
<title>Chat with Alpine Agent</title>
<style type="text/css">
<!--
.s1 {font-family: Arial; color:#333366; font-size:12px}

body {
     background-color: #FFFFFF;
}
-->
</style>

</head>

<body onload="self.location.href='#base'">
<p class="s1">
<%
dim objOpenFile, objFSO, strPath
strPath = Server.MapPath("./" & File_name & ".txt")
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
set objOpenFile = objFSO.OpenTextFile(strPath, 1)
While not objOpenFile.AtEndOfStream
      Response.Write objOpenFile.ReadLine
      Response.Write("<br />")
Wend
%>
<a id="bottom" name="base"><font color="#FFFFFF">Show this</font></a>
</p>
</body>
</html>

how bout this?
0
 
LVL 16

Author Comment

by:CWS (haripriya)
ID: 17954625
kelvinwkw,

The page still refreshes.
0
 
LVL 11

Accepted Solution

by:
kelvinwkw earned 500 total points
ID: 17962198
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim File_name
File_name = Request.QueryString("sFname")
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chat with Alpine Agent</title>
<script type="text/javascript">
<!--

function updateChat(){
 http_request = new ActiveXObject("Microsoft.XMLHTTP");
 http_request.open('GET', '<%=trim(File_name)%>.txt?dummy='+Math.random(), false);
 http_request.send();
 document.getElementById('chatlog').innerHTML = http_request.responseText;
 setTimeout("updateChat()", 5000);
}
//-->
</script>
<style type="text/css">
<!--
.s1 {font-family: Arial; color:#333366; font-size:12px}

body {
     background-color: #FFFFFF;
}
-->
</style>

</head>

<body onload="updateChat();">
<span class="s1" id="chatlog">
<%
dim objOpenFile, objFSO, strPath
strPath = Server.MapPath("./" & File_name & ".txt")
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
set objOpenFile = objFSO.OpenTextFile(strPath, 1)
While not objOpenFile.AtEndOfStream
      Response.Write objOpenFile.ReadLine
      Response.Write("<br />")
Wend
%>
</span>
<a id="bottom" name="base"><font color="#FFFFFF">Show this</font></a>
</body>
</html>


how bout this?
0
 
LVL 16

Author Comment

by:CWS (haripriya)
ID: 18156840
pinaldave,

Sorry for the delay.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question