xajax is an open source PHP class library that allows you to easily create powerful, web-based, Ajax applications using HTML, CSS, JavaScript, and PHP. (from http://xajax.sourceforge.net/)

It has a nice and straightforward implementation. It's still in alpha but that should be enough for most purposes.

So, here's a short howto for your own modules, f you like to implement some ajax stuff.

1. Download the class and place it into your module directory

2. Include it into your module file

 require_once('inc/xajax.inc.php' ); 

3. Write the code
Your website should do something when the user clicks the button ;)
We just take the function 'my_function'.

// place this into your php code
function my_function() {
  // code comes later
}

4. Register your functions
Place the following code into your [module_name]_menu() function ( What's this?)

    // initialise xajax
    $xajax = new xajax();
    
    // register functions for ajax
    $xajax->registerFunction("my_function");
    
    // include javascript in html header
    drupal_set_html_head($xajax->getJavascript()); 

    // let xajax process ajax requests
    $xajax->processRequests();

5. Write your javacript code
Wrong ;). We are not required to write any javascript code. Xajax is doing the dirty work for us.

6. Call the function
We want to call our function, if a user clicks a button.
So we add that button somewhere like this:

form_button('Call my_function','op' ,'submit' , array('onclick'=>'xajax_my_function(); return false;'));

So you just have to add a 'xajax_' in front of the name of your php function.

7. Write some more code
Now we still have to write the code w/in our php function 'my_function'.

function my_function() {
	  $objResponse = new xajaxResponse();   
          // make db queries.. save new content.. all that cool stuff why we learned php

          // some test output
	  $objResponse->addAlert("You pressed da button");
	  return $objResponse->getXML();
}

----

That's it. Now your module is using ajax to communicate with your script and no page reloading is needed anymore :D

Comments

ica’s picture

Sounds good.. Do you have a demo or anyone else implemented in Drupal already?
i am not coder and i am not sure how exactly to implement/hack the core Drupal or even to try without braking, but as an idea can other classes be used with the implementation you described above?
such as this
http://openrico.org/rico/
demos
http://openrico.org/rico/demos.page

or other avaliable class links on this site
http://www.ajaxmatters.com

zielgruppe’s picture

I didn't know about rico, but it looks really promising.

Xajax is used to map your drupal php functions to javascript calls. Rico seems to be a library of javascript functions. So you could call some rico functions (eg. for that drag&drop stuff) and if you have to save that stuff you would call the xajax function, which calls your drupal code :)

I am using xajax in a module which I currently develop. I applied for access to the cvs, I'll post it here when I am able to ubload my code.

micha_1977’s picture

looks very promising, im looking forward for your module

-micha
work in progress with Drupal 4.6: langmi.de

zielgruppe’s picture

the module can be found here

travischristopher’s picture

damn, who wouldn't like to add some cinematic effects to their drupal site, or get rid of paging all together by showing Results in a Scrolling LiveGrid...

travist’s picture

I just created a tutorial on how I created Ajax in Drupal 5.0. The tutorial is called Drupal and Ajax : A complete Tutorial.

I hope this helps!

Thanks,

Travis.

mikev’s picture

tidwell link is "page not found". It seems he placed links to it everywhere, all of which are bad.

mdroste’s picture

As I said im my post from sept, 5 node 30428 there are many AJAX API's we can use to add AJAX functions to Drupal. I used Sajax from www.modernmethod.com/sajax for my sample AJAX Web Directory

But I think we need ONE Api for this task. Maybe the core drupal developers have to choose one. If not we've got one module with this API and another with that API.

Sounds not good.
--
go with us - learn php

--
mdwp*

Dublin Drupaller’s picture

+1 from me.

I agree about having one API and it's probably something for the core developers to decide upon.
by the way. I liked that simple catalogue implenentation on the site you linked.

Very handy.

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Hosting Geek’s picture

+5 insiteful

zielgruppe’s picture

I used Xajax because I worked out of the box. But perhaps Sajax is even better. (I actually tried it out and dropped it, don't remember why)

mdroste’s picture

It's not a matter of which API is better. The only relevant question is: which API should we use for Drupal modules.
This should be only one - of course a good one.
(May be a new API only for Drupal)
I don't want to develop serious AJAX modules until this question is not fully answered.

--
go with us - learn php

--
mdwp*

youngwax’s picture

the file drupal-cvs/misc/drupal.js seems to have HTTPPost, HTTPGet cover functions. Next to it is collapse.js, which might be for the tricky pulldown menu thing. I guess before we all pull in different ajax tools, we could find out what is already on our site...and help each other with documentation & how-to.

dllh’s picture

This helped me quickly add some ajax functionality to a module I was working on. I hadn't looked at xajax enough previously to see how nice it actually is.

I needed xajax functionality in multiple modules but started getting "function cannot be redefined" collisions when I copied this code into multiple modules.

So I wrote a xajax module that you call to include the functionality only once and that looks for hooks in all loaded modules to register functions. The drill is as follows:

* Install and enable the xajax module
* In any module you want to add xajax functionality for, create a function named modulename_xajax_init. This function contains only the function register calls for the given module (and takes $xajax by reference)
* For each registered call, write the function you're registering and add any js calls to your ui.
* That's it.

I'm looking at getting this included as a contrib module (xajax's LGPL might be an issue), but in the mean time, it's available (short-term) here.

arthurf’s picture

I was checking this out a bit and came up with a way to do this fairly elegantly:

/**
 * Implementation of hook_help().
 */
function xajax_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Allows for xajax fuctions to operate');
  }
}

/**
 * Implementation of hook_menu().
 */
function xajax_menu($may_cache) {
  $path = drupal_get_path('module', 'xajax');
  require_once($path . "/xajax.inc.php");
  drupal_set_html_head("<scr1pt type=\"text/javascript\" src=\"". base_path() . $path . "/xajax_js/xajax.js\"></scr1pt>");
}

Note that I replaced the "i" in script with a 1.

I'm going to create some examples that people can use with drupal forms for people to check out.

ekitel’s picture

I'm trying to use this to add an element to a form, It seems to go smoothly, no error, but I don't see the new element anywhere on the page

function make_page() {
		  $objResponse = new xajaxResponse(); 
		  $form['testing'] = array( 
		    '#value' => 'testing.........', 
		  );
	  $objResponse->addAlert("You pressed da button");
	  return $objResponse->getXML();
 }
Jacob’s picture

Ranajee’s picture

Hello everybody!
I am working on website using Drupal. I have done a lot of things using it. Now working on my own custome module. This module gives simple CRUD (Create, Reterive, Update and Delete) operations to just meet the requirement. Now the problem I need to improve the functionality of this module. I have to implement xajax to make it more interactive. But after a long search and effort could not find the simple search like This .
So anybody can help me to make it Step-By-Step.

Help me.
---- Rana Jee----

zwu_ca’s picture

Could you give a button example in 4.7? No form_button is available. I didn't find any other button function (except radios).

aeeckhau’s picture

In 4.7 step 6 has to be like:

6. Call the function
We want to call our function, if a user clicks a button.
So we add that button somewhere like this:

$form['button'] = array('#type' => 'button',
        '#value' => t('submit'),
        '#attributes' => array('onclick' => 'xajax_my_function(); return false;'),
        );

So you just have to add a 'xajax_' in front of the name of your php function.

You also need something like

     $output = drupal_get_form('form_name', $form);
     return $output;

see also the Forms API Quickstart Guide

In step 4 I also altered:

 drupal_set_html_head($xajax->getJavascript()); 

into

 drupal_set_html_head($xajax->getJavascript($path_where_ajax_is)); 
zwu_ca’s picture

It takes too much memory on the server side -- some report also in the client side. I'm using .24 version.

travist’s picture

For anyone interested in this topic, I just created a tutorial on how to create your own custom Ajax goodness in Drupal 5.0. The tutorial is called Drupal and Ajax : A complete Tutorial.

I hope this helps someone!

Thanks,

Travis.

palabat’s picture

Hi Travis,

I'm in this situation where I have a navigation tree with many nodes (thousands). When the user clicks in any of these links (nodes), I want the server to serve the node content in the main content pane without refreshing the whole page including my block (tree). Also, I want to implement a cookie plugin to remember the state of the tree.
Is this possible in Drupal?

Thanks.
pol
Is this possible in

mikev’s picture

Not only did tidwell post this tutorial more than once, the link is bad. "page not found"

juinz’s picture

Hi, i made a module and i used xajax like it was implemented here. It runs well, but when i try to access other types of node (like pages) or modify elements, i get error: xajaxplugin redeclared... did u noticed something like this???

muso’s picture

I get a redeclare error also:

Fatal error: Cannot redeclare class xajaxplugin in /home/abc/public_html/modules/def/xajax_core/xajaxPlugin.inc.php on line 26

Still haven't figured out what's causing it yet.

zupafly’s picture

I'm getting the same error, same file, same line... what gives?

zupafly’s picture

Well the way I'm getting around this "Cannot redeclare class xajaxplugin..." error is by modifying line 24 in xajax_core/xajaxPlugin.inc.php to be like this

if (!class_exists('xajaxPlugin', false)) {
    class xajaxPlugin
    {
    }
}

Let me know if this works for you.

davemybes’s picture

That fix did not actually solve the problem. However, moving all the registerFunction statements out of the hook_menu section did. In fact, all the xajax calling is done outside of a function, at the top the module. This seems to have solved the "redeclare" problem...finally.
______________________________________________________________________________________________________
mybesinformatik.com - Drupal website development

______________________________________________________________________________________________________

kalhohan’s picture

Hi, I have successfully update the xajax module, which work now with drupal 6, and xajax 0.5 callable object notion.
Anyone interrested?

vaidmca’s picture

Hi,

As you mentioned that you have successfully implemented xajax in drupal 6, can you please send me the sample as I am not able to get it worked on my end.

Thanks in advance.

Sachin Vaid

sathish’s picture

I dont know anything about XAJAX.

I want everthing about the XAJAX

1. i want 2 download XJAX MODULE , where sholud i go and download the module.

2. i want to implement AJAX in dropdownlist.

3. if anybody have source code and tell me were 2 implement that one. where should i change the code in my module.

4. i want ajax in registration form in drupal. ( if username already exists in database means it should display it is avaliable change username )

plz reply me

George2’s picture

uhm, you do know jquery makes ajax dead simple. you do know that jquery is already available in drupal? the thought of having to add more excessive code to redo something that's already possible would make drupal uhm...bloated.

sathish’s picture

hi

i know Jquery syntax

but i dont know how 2 implement that in my module.

plz help me.

vaidmca’s picture

Hi,

As you mentioned that you have successfully implemented xajax in drupal 6, can you please send me the sample as I am not able to get it worked on my end.

Thanks in advance.

Sachin Vaid