Hello

I have a custom module with these implemenation :

the hook menu :

$items['getdetail'] = array(
    'title' => 'title',
    'page callback' => 'get_detail',
	'access callback' => TRUE,
	'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,    
  );

my simple function (not a ajax callback)

function get_detail() {
         drupal_add_library('system', 'drupal.ajax');
         $commands = array();
	 $commands[] = ajax_command_alert("Aucun resultat");
	 return array('#type' => 'ajax', '#commands' => $commands);
}

I have a list in table an tr

echo "<tr><td height=30'><input type='checkbox'/></td><td width=250 onmouseover='showDetail(".$val['nid'].");'>".$val['name'].""</td></tr>";	

and my js that call the function

function showDetail(nid) {
	jQuery.ajax({
			type: "POST",
            url: "getdetail",
            data: "nid="+nid,
			success: function(data) {
			
			}
            
	});
	
}

but alert (ajax_command_alert) is not working

What is wrong in my code ?

Thank you very much for your help

Comments

tce’s picture

Firstly, I'm not an expert, but I have a feeling it's because commands need to be done through Drupal.ajax, of which you need to create a new instance of. Commands are called in the /includes/ajax.js file via the Drupal.ajax.prototype.commands object, perhaps you could call that directly? Though by not using Drupal.ajax properly you won't be updating CSS and JS in your page callback if you decide to add them in future.

stomerfull’s picture

I've tried this for a test but not working :
PHP

function my_ajax_callback() {
  $data = 'saved!';
  // instead of using ajax_command_html, we provide our own 
  // js custom callback function
  $output = array(
    '#type' => 'ajax',
    '#commands' => array(
      array('command' => 'myjscallback', 'data' => $data),
    ),
  );

  return $output; 
}

JS :

$(function() {
  Drupal.ajax.prototype.commands.myjscallback = function (ajax, response, status) {
    $('#save-btn-message').html('<span>' + response.data + '</span>');
  };
});

i m getting this :
Drupal.ajax.prototype.commands.myjscallback is undefined

thank you very much for your help

stomerfull’s picture

i also try to add this inside my module

drupal_add_library('system', 'drupal.ajax');
drupal_add_library('system', 'jquery.form');

describe here http://api.drupal.org/api/drupal/includes!ajax.inc/group/ajax/7#comment-...

but it not working

tce’s picture

Whenever I use commands I use ajax_render(), which returns everything in the correct JSON format, maybe that's what you are missing.

I'm not really sure what you mean by 'non callback function', it seems that it is a page callback function. I believe you can do what you are trying to do, but I think it would be better to do it through Drupal.ajax. For example, I would create a new instance and bind the event to the table item, something like...

(function($) {

Drupal.behaviors.example = {
  attach: function(context, settings) {

    var nid = 1; // need some way to read the nid, perhaps by attaching the node id using the rel attribute to the table item.

    $('your_selector_here')
      .addClass('use-ajax')
      .addClass('ajax-processed');
    var element_settings = {};
    element_settings.progress = { 'type': 'throbber' };
    element_settings.url = '/getdetail/'+ nid;
    element_settings.event = 'mouseover';
    var base = $this.attr('id');
    Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);

  }
};
  
})(jQuery);

Then I would change your menu item slightly...

<?php
$items['getdetail/%node'] = array(
  'title' => 'title',
  'page callback' => 'get_detail',
  'page arguments' => array(1), // pass node data to function
  'access callback' => TRUE,
  'access arguments' => array('access content'),
  'type' => MENU_CALLBACK,    
);
?>

Then in your page callback function you could do...

<?php
function get_detail($nid) {
  // $nid should be a node object
  $commands = array();
  $commands[] = ajax_command_alert("Aucun resultat");
  print ajax_render($commands);
  exit;
}
?>

Also, I don't think there is any need putting drupal_add_library('system', 'drupal.ajax'); in the page callback function, as that needs to go into whatever page your table is in instead.