4 Replies - 4940 Views - Last Post: 20 January 2009 - 03:37 PM

#1 eXsolved  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-January 09

Very Simple AJAX/JavaScript DIV Update

Posted 16 January 2009 - 12:44 AM

Hello Guys

I am creating my first PHP web chat box. And it works great. Now, I am stuck with how to update the div that displays the chats automatically without the user having to refresh the page. This is my div:

<div id="chatbox" class="msg_box">
				<? include( "chatlog.htm" ); ?>
			</div>

			<script type="text/javascript">
				var objDiv = document.getElementById("chatbox");
				objDiv.scrollTop = objDiv.scrollHeight;
			</script>


The chatlog.htm file gets updated every time a user enters a message & hits submit. And when they DO hit submit, the page is reloaded and that little Javascript under the div scrolls down to the last message. But when they're not hitting submit, nothing happens and they wont know someone else has posted unless they refresh the page.

Can I do this refresh automatically with Ajax? If yes, how?

Thanks,
eXsolved

Is This A Good Question/Topic? 0
  • +

Replies To: Very Simple AJAX/JavaScript DIV Update

#2 parker_as3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-January 09

Re: Very Simple AJAX/JavaScript DIV Update

Posted 19 January 2009 - 02:15 AM

id start by looking into the XMLHttpRequest object there is a ton of documentation out there on this subject that would be the solution to your problem. alistapart.com has a good article on it.
Was This Post Helpful? 0
  • +
  • -

#3 kiwi2  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 178
  • Joined: 16-September 08

Re: Very Simple AJAX/JavaScript DIV Update

Posted 20 January 2009 - 09:09 AM

you cld actually use php to refresh the page
eg using a timed reload, as for ajax, some action on the part of the user is still necessary to send javascript data to the server, bu it would certainly assist your application, there r hundreds of articles to get you started try javascript.internet.com for some direction
Was This Post Helpful? 0
  • +
  • -

#4 eXsolved  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-January 09

Re: Very Simple AJAX/JavaScript DIV Update

Posted 20 January 2009 - 03:25 PM

Thanks I've managed to figure it out with some very simple ajax. I also have it so it's automatically updating without any user input with the command:

window.setInterval("doIt()", 200);

doIt(); is just my ajax function that updates the div.

Yay Ajax :wub:
Was This Post Helpful? 0
  • +
  • -

#5 JohnWaters  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 140
  • Joined: 28-November 08

Re: Very Simple AJAX/JavaScript DIV Update

Posted 20 January 2009 - 03:37 PM

You could have a polling Ajax request. Requesting every 10 seconds or so and have the server return either the new post(s) or nothing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1