PDA

View Full Version : Intergrating AJAX Technology Into Your Modifications


Zero Tolerance
31 Jul 2005, 02:34
This How-To will show you how to use the defined AJAX functions within vBulletin so developers can see how easily it is to intergrate AJAX technology into modifictions. Please note this tutorial is aimed at developers with some javascript knowledge.

Step 1 - Understand AJAX:

As many of you will know, AJAX gives the ability to pass data through a stream and recieve any data returned with no reloading, so if you're thinking of intergrating AJAX then you must first have a scenario where you wish to push data and recieve it, recieval is not required however is probably the best part of AJAX in a users opinion due to the dynamic changes that occur to present the recieved data.

Step 2 - Setting Up AJAX:

Okay, so you want to set up AJAX, real easy, the first step is to create an AJAX Object, this can be done with ease using the vBulletin set-up, an example is below:


Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



You'll notice the one and only passive variable here for the function 'vB_AJAX_Handler' is set to 'true', this determines wether to use asynchronous or not, set to false if you do not desire to use it, if you're unsure leave this set to true.

So now you have your AJAX Object set up, before firing data you'll need to to set the object to respond to a function for when it recieves data, an example is below:


Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



Here the parameter is not so much a variable, but infact a function name, in this case i put 'My_AJAX_Reciever', so that function must exist, an example function is below:


Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



This function pretty much checks the stream has loaded and sent data back to the browser, upon success it will alert you to say it was successful, and the data it recieved. How you handle the data is upto you.

Step 3 - Firing And Recieving Data:

So now you want to fire some data through AJAX, and get some back, let's first look how we fire data:


Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



Let's take a quick overlook at the parameters used here:

Parameter 1 - The file you want to send the data to.
Parameter 2 - Post variables you want to send through.


In most cases your post variables (parameter 2) would fluxuate, and you may want to send some variables that users have inputted, if so you may want to take advantage of vBulletin's javascript emulation of urlencode(), first off you'd parse the variable in javascript before firing off, as shown below:


Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



This makes the variable safe for the stream to send the data, however some characters will be broken, to fix these in your php when collecting the variable you would parse it like so:


Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



Okay, so you've now fired data off to 'myfile.php', with the variables: do (ajax) and pagevar (text). Now comes the PHP part, where you handle the data and output some back to be recieved:


Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



Simple as that? Pretty much, ofcourse the desired data to be sent back due to the data sent is completely up to you, if you've set the javascript up with the examples used above you should get a message saying:
Data recieved successfully

text

So if you're not sure, what the PHP outputs AJAX grabs, and just to ensure the script ends there it's best to make PHP stop execution of any code underneath by using exit;, this ofcourse it not required, just likely in most situations :)
Remember, to grab using AJAX what PHP outputted, the code is My_AJAX_Object.responseText

Final Line - Handling Data and Dynamics:

So you've managed to successfully fire some data off, and got some back, now you want to fiddle with it in Javascript and make changes to your page?
Then i'm afraid you're on your own for that job, how you handle the recieved data is completely upto you and the scenario you're using AJAX in.

If you wish to see more information on AJAX, i suggest you look at this tutorial i wrote a while ago for vBulletin.org, it includes more information on AJAX than posted here :)

http://www.vbulletin.org/forum/showthread.php?t=81626

Any questions/problems reply here and i'll try to resolve them for you, the code above is all written off the top of my head, but i don't see any errors myself, feel free to report them to me though :)

- Zero Tolerance

Christine
31 Jul 2005, 03:00
Thank you!! :)

calorie
31 Jul 2005, 03:02
Yep, thanks for the toot. :)

Brad
03 Aug 2005, 11:23
You'll notice the one and only passive variable here for the function 'vB_AJAX_Handler' is set to 'true', this determines wether to use asynchronous or not, set to false if you do not desire to use it, if you're unsure leave this set to true.

Just to add to this:

Asynchronously:

Processing is done in the backround and allows the user to use do other things on the page while the request is begin made and returned. You should use this is most cases

Synchronously:

Page is 'locked' until request is made and data is returned to the browser. This should not be used often because if there is a connection timeout or other issues the user will be left with a page that will not function.

yoyoyoyo
03 Aug 2005, 12:12
excellent info! thanks

TCM
07 Aug 2005, 07:11
This helps a lot, thanks!

Logikos
23 Aug 2005, 23:59
I have a question/problem. I've read this over and over untill my eyes hurt, and I can't seem to get it to work. Here is the code I'm using:


test.js includes:


Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



My PHP file contains:


Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------




I don't understand what I place in the HTML part? Could you please show some code example. The PHP/Java/HTML. :) Thanks

Zero Tolerance
27 Aug 2005, 23:49
Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



No doubt you'll get a JS error on that line, because "My_Variable" is undefined, you can remove that line.

Also the HTML would just include the JS file, ie:


Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



As I stated in the main post, getting the JS to change the html on the page dynamically is up to you, it is recommended you know Javascript really. If you would like to look at a working example i'd suggest looking at my vBShout for 3.5 really.

- Zero Tolerance

Logikos
28 Aug 2005, 01:20
Thanks Scott, I've been reading alot of tutorials, and I've been studying your code with your shoutbox hack. I'm slowly getting the hang of it, but it is going to take alot of time.

I'm in the middle of a project and I am making the core of the project all ajax. I've figured out how to do a few things; one thing I’m trying to do is open new pages without reload. I see GMail does that, and I believe it has to do with Iframes, but I’m not sure. I'll get it eventually, I've pored many late nights reading so, I better get it soon! :p

jugo
28 Aug 2005, 03:31
I see GMail does that, and I believe it has to do with Iframes


LiveWire

i think it has more to do with DIVs than iFrames....but i could be wrong.

Akex
03 Sep 2005, 23:44
First, thank you for this tutorial, very helpful.

You forgot to put one key world in your javascript code : handler

So this code :



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



Becomes



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------

;)

harmor19
02 Oct 2005, 04:04
I did the code above but it didn't work at first.
I changed the $_POST to $_GET then I passed test.php?do=ajax&pagevar=rrr in the address bar and it worked.
But using $_GET is insecure.

Can someone make a small working example like a simple chat?

Jenta
02 Oct 2005, 04:38
look at vbshout
its like a simple chat

DaFire
26 Dec 2005, 15:46
the missing .handler. should really added to the first posts example ;)

PennylessZ28
27 Dec 2005, 00:47
this was very useful, thanks

DaFire
28 Dec 2005, 20:06
yes ;) it's great start to integrate own ajax things.. I used rico.js but looks like it creates problems with some vb javascript so I change everything to vb-ajax now :)

MP3
25 Jun 2006, 21:33
For the JavaScript written in post 7 by " LiveWire " how can we add auto refresh so every 5 min it will check certain variables in a PHP file if its exist then small popup will shown if no nothing will happen and all of this will happen without the manually refresh.

rogersnm
02 Jul 2006, 15:49
so for the:

Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------


could i change it to:


Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------

if $variable is defined in the global_start?

Psionic Vision
02 Jul 2006, 16:44
Yes.

rogersnm
02 Jul 2006, 16:53
Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



So once i have done that can i just put $suck in a template and it will automatically refresh when the user clicks a link?

What should i use for onclick?: eg <a href='#' onclick='return Who knows'>Refresh</a>

DOES ANYONE HAVE A WORKING EXAMPLE OF THIS?

Andrew Green
02 Aug 2006, 19:02
DOES ANYONE HAVE A WORKING EXAMPLE OF THIS?


ok, here is a VERY stripped down demo as a standalone file, just put it in your main forum folder (ex. "forum") and name it "ajax.php"

All it does is give you the current time on a 1 sec refresh. Not something particullarly useful as there are far better ways to do this, but hopefully it helps.




Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------

rogersnm
02 Aug 2006, 21:08
i did get it to work since whenever i posted that which was a month ago today..

Andrew Green
02 Aug 2006, 22:17
Well maybe someone else will find it useful then :)

rogersnm
03 Aug 2006, 08:09
the timer thing is useful though :)

Andrew Green
03 Aug 2006, 14:51
the timer thing is useful though :)


And as a side note on that, timers on main pages can be dangerous, especially with low timeouts. Server load goes up.

Renmiri
06 Aug 2006, 01:11
Great tutorial, thanks!

rogersnm
06 Aug 2006, 17:40
not if you set it for 15 seconds (well, not as much.)

HaMaDa4eVeR
18 Aug 2006, 14:59
great topic :)

what's the better way to pick the members list throgh AJAX,
example: if I've in my hack's field require to input members username, what I should to do to bring the similer username,

HaMaDa4eVeR
27 Aug 2006, 08:54
Up

are you there

Wongod
29 Sep 2006, 08:25
Is there anything special that you need to do to get ajax working on non-forum pages? Does vbulletin has some kind of security that prevents it from giving responses to files outside the forum directory? Because I have not been able to get it to work from my root directory. It will send out but never come back with a response.

rogersnm
27 Oct 2006, 11:48
Are you sure it sends out did you include the JS file which homes the ajax wrapper? You could of course just code your own ajax wrapper.

mrpaint
27 Oct 2006, 13:57
Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



I added this script but I have got this error message:
Error: do_box is not defined
Line: if (do_box.handler.readyState == 4 && do_box.handler.status == 200) {
And this one
Error: do_button is not defined
Line: if (do_button.handler.readyState == 4 && do_button.handler.status == 200) {

Anybody help me?

PS: I posted this question here (http://www.vbulletin.org/forum/showpost.php?p=1104687&postcount=17) but no body answered....

mokujin
21 Jul 2008, 23:32
ok, here is a VERY stripped down demo as a standalone file, just put it in your main forum folder (ex. "forum") and name it "ajax.php"

All it does is give you the current time on a 1 sec refresh. Not something particullarly useful as there are far better ways to do this, but hopefully it helps.




Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------


It doesnt work for me! It shows the time when loaded the page only

IdanB
08 Jul 2009, 18:25
Thank you !!
Excellent tutorial guide, very useful information :)