Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to change it to AJAX

Posted on 2006-11-15
8
Medium Priority
?
194 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
Industry Leaders: 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 2000 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

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!

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

670 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