Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles

Reply
 
Thread Tools
Intergrating AJAX Technology Into Your Modifications
Zero Tolerance's Avatar
Zero Tolerance
Join Date: Feb 2004
Posts: 813

 

England
Show Printable Version Email this Page Subscription
Zero Tolerance Zero Tolerance is offline 07-30-2005, 10:00 PM

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:
My_AJAX_Object = new vB_AJAX_Handler(true);
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:
My_AJAX_Object.onreadystatechange(My_AJAX_Reciever);
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:
function My_AJAX_Reciever()
{
	if (My_AJAX_Object.handler.readyState == 4 && My_AJAX_Object.handler.status == 200 && My_AJAX_Object.handler.responseText)
	{
		alert('Data recieved successfully\n\n' + My_AJAX_Object.handler.responseText)
	}
}
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:
My_AJAX_Object.send('myfile.php', 'do=ajax&pagevar=text');
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:
My_Variable = PHP.urlencode(My_Variable);
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:
PHP Code:
$My_Variable convert_urlencoded_unicode($My_Variable); 
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:
PHP Code:
if ($_POST['do'] == 'ajax')
{
    echo 
$_POST['pagevar'];
    exit;

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:
Quote:
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

https://vborg.vbsupport.ru/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
Reply With Quote
  #2  
Old 07-31-2005, 01:00 AM
Christine's Avatar
Christine Christine is offline
 
Join Date: Oct 2001
Location: PA
Posts: 472
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you!!
Reply With Quote
  #3  
Old 07-31-2005, 01:02 AM
calorie calorie is offline
 
Join Date: May 2003
Posts: 2,804
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yep, thanks for the toot.
Reply With Quote
  #4  
Old 08-03-2005, 09:23 AM
Brad Brad is offline
 
Join Date: Nov 2001
Posts: 4,765
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Zero Tolerance
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.
Reply With Quote
  #5  
Old 08-03-2005, 10:12 AM
yoyoyoyo's Avatar
yoyoyoyo yoyoyoyo is offline
 
Join Date: Dec 2004
Location: USA
Posts: 1,612
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

excellent info! thanks
Reply With Quote
  #6  
Old 08-07-2005, 05:11 AM
TCM TCM is offline
 
Join Date: Dec 2004
Location: 3.6.8
Posts: 75
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

This helps a lot, thanks!
Reply With Quote
  #7  
Old 08-23-2005, 09:59 PM
Logikos Logikos is offline
 
Join Date: Jan 2003
Posts: 2,924
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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:
My_AJAX_Object = new vB_AJAX_Handler(true);
My_AJAX_Object.onreadystatechange(My_AJAX_Reciever);

function My_AJAX_Reciever()
{
	if (My_AJAX_Object.readyState == 4 && My_AJAX_Object.status == 200 && My_AJAX_Object.responseText)
	{
		alert('Data recieved successfully\n\n' + My_AJAX_Object.responseText)
	}
}

My_Variable = PHP.urlencode(My_Variable);
My_AJAX_Object.send('test.php', 'do=ajax&pagevar=text');
My PHP file contains:
PHP Code:
<?php

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('THIS_SCRIPT''test');

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();

// get special data templates from the datastore
$specialtemplates = array();

// pre-cache templates used by all actions
$globaltemplates = array();

// pre-cache templates used by specific actions
$actiontemplates = array();

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');

$My_Variable convert_urlencoded_unicode($My_Variable);  

 if (
$_POST['do'] == 'ajax')
{
     echo 
$_POST['pagevar'];
    exit;
}  

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" lang="en">
<head>
<script type="text/javascript" src="clientscript/test.js"></script>
    <title>Test</title>
</head>
<body>

What Do I place here??????

I don't understand what I place in the HTML part? Could you please show some code example. The PHP/Java/HTML. Thanks
Reply With Quote
  #8  
Old 08-27-2005, 09:49 PM
Zero Tolerance's Avatar
Zero Tolerance Zero Tolerance is offline
 
Join Date: Feb 2004
Location: England
Posts: 813
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Code:
My_Variable = PHP.urlencode(My_Variable);
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:
HTML Code:
<script type="text/javascript" src="clientscript/test.js"></script>
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
Reply With Quote
  #9  
Old 08-27-2005, 11:20 PM
Logikos Logikos is offline
 
Join Date: Jan 2003
Posts: 2,924
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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!
Reply With Quote
  #10  
Old 08-28-2005, 01:31 AM
jugo jugo is offline
 
Join Date: Feb 2004
Location: Reading your emails.
Posts: 573
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Live Wire
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.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:08 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04806 seconds
  • Memory Usage 2,311KB
  • Queries Executed 23 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (7)bbcode_code
  • (1)bbcode_html
  • (3)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (10)post_thanks_box
  • (3)post_thanks_box_bit
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (10)post_thanks_postbit_info
  • (9)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete