Jump to content

Don't know if AJAX is the answer...


AV1611

Recommended Posts

Hi,

 

I wrote a shoutbox in PHP.

 

It works very well except one problem:

 

The box is a div that shows the posts and a textarea at the bottom. the whole thing uses a meta refresh at 30 seconds...

 

Now, the problem is if you are typing in the box, it will refresh, and that's annoying cause you never really have enough time to finish your shout it seems like...

 

So, keep in mind I know ZERO js or ajax, only PHP... The top DIV content comes from mysql and has a ton of filters for smileys, bad words, bbcode, etc... already in it...

 

I would like the top div to refresh independently of the bottom... sure, I could put the top in an iframe... and put the meta refresh in there, but and iframe in and iframe(the shoutbox is part of a bigger page) that just seems kinda lame.

 

Can someone help?

Link to comment
Share on other sites

don't use meta refresh, use the setInterval or setTimeout function in javascript, and when the focus is on the shoutbox i tihink you use clearTimeout.. below is two lines that i have used in the past. Hope it helps.

 

alertTimerId = setTimeout(sendRequest2('ajaxhottime',0), 30000);

clearTimeout(alertTimerId);

Link to comment
Share on other sites

Thank you and I will try and implement what you suggest...

 

Please note however I know ZERO js.  None. Nada. I know PHP/HTML/CSS.  I just have to do a client side not server side for this problem is the only reason I'm looking at js or ajax

 

hints welcome!

Link to comment
Share on other sites

look at the prototype ajax work.

 

http://prototypejs.org/learn/introduction-to-ajax

 

function sendRequest2(divframe, idx) {
	glbVar2 = divframe;	
	clearTimeout(alertTimerId);			
	new Ajax.Request("includes/ajax_alt.php?mode=stop&id="+idx, 
	{ 			
		method: 'post', 
		onComplete: showResponse2
	});

	return false;
}

function showResponse2(req)
{

	clearTimeout(alertTimerId);	
	document.getElementById(glbVar2).innerHTML= req.responseText;
	clearTimeout(alertTimerId);	
	alertTimerId = setTimeout(sendRequest2('ajaxhottime',0), 30000);

}

with one of my real life work. divframe is the Id of your div. The ajax_alt.php file is one that your have to create and call your php in that. In this case i am posting 2 get values in order for me easily use ajax without creating 100 dfifferent php files. but you should be able to do something good.

 

note, do  note return a php value it won't work. print or echo everything in your PHP file.

Link to comment
Share on other sites

  • 2 weeks later...

Thank you so very much. I feel impotent.  I just have no idea what to do with the code sample you provided. I don't even know JS just php.

 

Unless a kind person wants to help me understand how to implement the answer I'll have to go away... sigh...  :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.