Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    XMLHttpRequest Help

    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:

    Code:
    <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.
    Last edited by Shwaza; 05-24-2005 at 08:33 PM.

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #3
    New Coder
    Join Date
    May 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm talking about XMLHttpRequest, do you know what that is? I don't want anything to do with refreshes.

  • #4
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes,
    i know what XMLHTTPRequest is.

    still don't understand your question.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #5
    New Coder
    Join Date
    May 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How could i use XMLHttpRequest for my chat without having to use refresh.

  • #6
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #7
    New Coder
    Join Date
    May 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #8
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    see, now that i know the question YES, i can help

    Code:
    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;
    }
    Code:
    <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/
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #9
    New Coder
    Join Date
    May 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That script didn't exactly do anything...

    Here's what i have:

    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()">
    
    </body>
    
    
    </html>
    Did i do something wrong?

  • #10
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    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>
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #11
    New Coder
    Join Date
    May 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

    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;
    window.setTimeout("getChat()", 3000);
    }
    </script>
    
    </head>
    
    
    <body bgcolor="#FFFFFF" onload="getChat()">
    <div id="chatText">
    
    </div>
    </body>
    
    
    </html>
    Thanks

  • #12
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    move the setTimeout to before the return.

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

    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)
    window.setTimeout("getChat()", 3000);
    return;
    }
    </script>
    
    </head>
    
    
    <body bgcolor="#FFFFFF" onload="getChat()">
    <div id="chatText">
    
    </div>
    </body>
    
    
    </html>
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #13
    New Coder
    Join Date
    May 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #14
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #15
    New Coder
    Join Date
    May 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •