...

View Full Version : XMLHttpRequest Help



Shwaza
05-23-2005, 02:53 AM
EDIT: I've started to get it, and i have a file ajax_chat.php that has the chat in it, and here's what i have for ajax_chat.htm:



<html>
<head>
<title></title>

<script language="javascript" type="text/javascript">

var x=3;
var y=1;

function timer(){
x=x-y;
setTimeout("timer()", 1000);
if(x==0){

var url = "ajax_chat.php"; // The server-side script

function handleHttpResponse() { if (http.readyState == 4) {

results = http.responseText;

}
}

function showchat() {
http.open("GET", url, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}


function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object

showchat();

}
}

</script>

</head>


<body bgcolor="#FFFFFF" onload="timer()">

</body>


</html>


I've used bits and peices from tutorials, however i can't seem to get it to work, can anyone figure out why?
Also, the below posts are about the post that was here before, just reply about this one please.

rlemon
05-23-2005, 06:46 AM
i'm not really understanding what you mean by "real time" timer?

is 3 seconds not 3 seconds no matter how you do it? as long as the time is exactly 3 seconds and the page refreshes..

maybe i'm missing something?

Shwaza
05-23-2005, 02:17 PM
I'm talking about XMLHttpRequest, do you know what that is? I don't want anything to do with refreshes.

rlemon
05-23-2005, 05:06 PM
yes,
i know what XMLHTTPRequest is.

still don't understand your question.

Shwaza
05-23-2005, 06:16 PM
How could i use XMLHttpRequest for my chat without having to use refresh.

rlemon
05-24-2005, 04:39 PM
i don't know that, and i'm not saying you do that. however i still don't know what 'real time chat' and xmlHTTPRequest have to do with eachother. your not being specific with your question.

so if i'm correct your building a site chat program and you need it to refresh every 3 seconds to keep people up-to-date in the chat?

and this is what you want to do correct?
now if thats all then it's easy. if (and this is what i think your asking for) you need to update the chat, but not reload the page. then we can work out something.

Shwaza
05-24-2005, 09:12 PM
Yes, that's what i'm trying to do.

All i need to do, is use XMLHttpRequest in the file chat.htm to just print out all the information made by chat.php, and every 3 seconds update that information. Can you help?

rlemon
05-24-2005, 09:41 PM
see, now that i know the question YES, i can help



function getChat(){
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new
ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}

var doc = "location/of/chat.html"; // or chat.php
xmlhttp.open("GET", doc, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
var chat = document.getElementById('chatText');
var html = xmlhttp.responseText;
chat.innerHTML = html;
}
}
xmlhttp.send(null)
return;
}




<div id="chatText">
the source of the external chat file will be placed within the div tags.
</div>


you can see a similar example here:
www.rlemon.com/tabbed/

Shwaza
05-24-2005, 09:52 PM
That script didn't exactly do anything...

Here's what i have:


<html>
<head>
<title></title>

<script language="javascript" type="text/javascript">

function getChat(){
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new
ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}

var doc = "ajax_chat.php"; // or chat.php
xmlhttp.open("GET", doc, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
var chat = document.getElementById('chatText');
var html = xmlhttp.responseText;
chat.innerHTML = html;
}
}
xmlhttp.send(null)
return;
}

</script>

</head>


<body bgcolor="#FFFFFF" onload="getChat()">

</body>


</html>

Did i do something wrong?

rlemon
05-24-2005, 10:34 PM
well,

i'm assuming your returning

document.getElementById(..) is null or not an object.

you need to include a place for the chat output to be sent to.

look in my previous post, i encluded some 'div' code.

here, ya know what, try this code


<html>
<head>
<title></title>

<script language="javascript" type="text/javascript">

function getChat(){
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new
ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}

var doc = "ajax_chat.php"; // or chat.php
xmlhttp.open("GET", doc, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
var chat = document.getElementById('chatText');
var html = xmlhttp.responseText;
chat.innerHTML = html;
}
}
xmlhttp.send(null)
return;
}

</script>

</head>


<body bgcolor="#FFFFFF" onload="getChat()">
<div id="chatText">

</div>
</body>


</html>

Shwaza
05-25-2005, 02:16 AM
Ah :) Thank you! It works great, however i'm having trouble getting a timer to work :S

Does anyone know why this code won't make it automatically redo this every 3 seconds?



<html>
<head>
<title></title>

<script language="javascript" type="text/javascript">

function getChat(){
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new
ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}

var doc = "ajax_chat.php"; // or chat.php
xmlhttp.open("GET", doc, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
var chat = document.getElementById('chatText');
var html = xmlhttp.responseText;
chat.innerHTML = html;
}
}
xmlhttp.send(null)
return;
window.setTimeout("getChat()", 3000);
}
</script>

</head>


<body bgcolor="#FFFFFF" onload="getChat()">
<div id="chatText">

</div>
</body>


</html>


Thanks :)

rlemon
05-25-2005, 02:25 AM
move the setTimeout to before the return.

it's jumping out of the function befor it has a chance to reload.



<html>
<head>
<title></title>

<script language="javascript" type="text/javascript">

function getChat(){
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new
ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}

var doc = "ajax_chat.php"; // or chat.php
xmlhttp.open("GET", doc, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
var chat = document.getElementById('chatText');
var html = xmlhttp.responseText;
chat.innerHTML = html;
}
}
xmlhttp.send(null)
window.setTimeout("getChat()", 3000);
return;
}
</script>

</head>


<body bgcolor="#FFFFFF" onload="getChat()">
<div id="chatText">

</div>
</body>


</html>

Shwaza
05-25-2005, 03:01 AM
Ok, there's only one problem now. It starts an infinite loop instead of replacing the previous data taken from ajax_chat.php

Do you know of a way to clear that data, then reload it every time the timer resets?

Also thank you so much for your help so far :D

rlemon
05-25-2005, 03:06 AM
do you mean it's appending the code to the previous code?

like lets say in the file ajax_chat.php is the text (and only the text)

'text'

is it doing this:
pass#1:text
pass#2:texttext
pass#3:texttexttext
ect... ect...

Shwaza
05-25-2005, 03:09 AM
Yes kinda, say it was saying 'text' it would be like

text
(3 seconds later)text
(3 seconds later)text


etc. Instead of replacing the text.

rlemon
05-25-2005, 03:11 AM
at the top of the function put

var html='';

and if that doesn't work check your php file?

the code looks fine.

Shwaza
05-25-2005, 03:14 AM
Sorry, it's still doing it. Also, i checked my php file, and when i view it, it works fine :S

rlemon
05-25-2005, 03:38 AM
hrmm, can you post a sample link?

Shwaza
05-25-2005, 12:20 PM
No because i just have it running locally at the moment. Also though, i tried using a different timer and it worked fine :)

Thanks for all your help though :)

rlemon
05-25-2005, 03:01 PM
no problem.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum