vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 3 Articles (https://vborg.vbsupport.ru/forumdisplay.php?f=187)
-   -   Intergrating AJAX Technology Into Your Modifications (https://vborg.vbsupport.ru/showthread.php?t=93249)

Zero Tolerance 07-30-2005 10:00 PM

Intergrating AJAX Technology Into Your Modifications
 
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

Christine 07-31-2005 01:00 AM

Thank you!! :)

calorie 07-31-2005 01:02 AM

Yep, thanks for the toot. :)

Brad 08-03-2005 09:23 AM

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.

yoyoyoyo 08-03-2005 10:12 AM

excellent info! thanks

TCM 08-07-2005 05:11 AM

This helps a lot, thanks!

Logikos 08-23-2005 09:59 PM

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

Zero Tolerance 08-27-2005 09:49 PM

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

Logikos 08-27-2005 11:20 PM

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 08-28-2005 01:31 AM

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.


All times are GMT. The time now is 01:05 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.01515 seconds
  • Memory Usage 1,770KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (7)bbcode_code_printable
  • (1)bbcode_html_printable
  • (3)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete