You can call a module function from the jquery ajax function to perform some database related operations based on ajax.
To start, create a menu item using hook_menu
function mymodule_menu() {
$items = array();
// Create a path to send our AJAX request to.
$items['mymodule_ajax'] = array(
'title' => 'menu title',
'page callback' => 'mymodule_ajax_function',
'access arguments' => array('access example ajax'),
'type' => MENU_CALLBACK,
);
return $items;
}
function mymodule_ajax_function(){
// Here you can perform your database operations
$var = "testing";
return drupal_json($var);
}
And then add a js file to your module and paste the following code.
Drupal.behaviors.mymodule=function(context){
//click event
jQuery.ajax({
url: "mymodule_ajax",
type: "POST",
dataType: "json",
success: function(data){
alert(data);
}
});
}
Cheers
To start, create a menu item using hook_menu
function mymodule_menu() {
$items = array();
// Create a path to send our AJAX request to.
$items['mymodule_ajax'] = array(
'title' => 'menu title',
'page callback' => 'mymodule_ajax_function',
'access arguments' => array('access example ajax'),
'type' => MENU_CALLBACK,
);
return $items;
}
function mymodule_ajax_function(){
// Here you can perform your database operations
$var = "testing";
return drupal_json($var);
}
And then add a js file to your module and paste the following code.
Drupal.behaviors.mymodule=function(context){
//click event
jQuery.ajax({
url: "mymodule_ajax",
type: "POST",
dataType: "json",
success: function(data){
alert(data);
}
});
}
Cheers