Error message

  • Notice: Trying to access array offset on value of type int in element_children() (line 6609 of /home1/tylerfra/public_html/includes/common.inc).
  • Notice: Trying to access array offset on value of type int in element_children() (line 6609 of /home1/tylerfra/public_html/includes/common.inc).
  • Notice: Trying to access array offset on value of type int in element_children() (line 6609 of /home1/tylerfra/public_html/includes/common.inc).
  • Notice: Trying to access array offset on value of type int in element_children() (line 6609 of /home1/tylerfra/public_html/includes/common.inc).
  • Notice: Trying to access array offset on value of type int in element_children() (line 6609 of /home1/tylerfra/public_html/includes/common.inc).
  • Notice: Trying to access array offset on value of type int in element_children() (line 6609 of /home1/tylerfra/public_html/includes/common.inc).
  • Notice: Trying to access array offset on value of type int in element_children() (line 6609 of /home1/tylerfra/public_html/includes/common.inc).
  • Notice: Trying to access array offset on value of type int in element_children() (line 6609 of /home1/tylerfra/public_html/includes/common.inc).
  • Notice: Trying to access array offset on value of type int in element_children() (line 6609 of /home1/tylerfra/public_html/includes/common.inc).
  • Notice: Trying to access array offset on value of type int in element_children() (line 6609 of /home1/tylerfra/public_html/includes/common.inc).
  • Notice: Trying to access array offset on value of type int in element_children() (line 6609 of /home1/tylerfra/public_html/includes/common.inc).
  • Notice: Trying to access array offset on value of type int in element_children() (line 6609 of /home1/tylerfra/public_html/includes/common.inc).
  • Notice: Trying to access array offset on value of type int in element_children() (line 6609 of /home1/tylerfra/public_html/includes/common.inc).
  • Deprecated function: implode(): Passing glue string after array is deprecated. Swap the parameters in drupal_get_feeds() (line 394 of /home1/tylerfra/public_html/includes/common.inc).

Drupal - Post Data via Ajax to PHP Menu Callback with JQuery and JSON

Category: 

Drupal 7

I found this solid example for Drupal 7.

http://www.computerminds.co.uk/drupal-code/make-link-use-ajax-drupal-7-its-easy

The code for D7 is much more elegant, there are some great new features in D7 to make this stuff easier.

Drupal 6

Put this PHP code inside your module, e.g. sites/all/modules/my_module/my_module.module

drupal_add_js(drupal_get_path("module","my_module") . "/my_module.js");

function my_module_perm () {
  return array(
    'access my_module via ajax',
  );
}

function my_module_menu () {
	
  $items = array();
	
  $items['my_module/example'] = array(
    'page callback' => 'my_module_example',
    'access arguments' => array('access my_module via ajax'),
    'type' => MENU_CALLBACK,
  );
	
  return $items;
}

function my_module_example () {
  if (empty($_POST["js"])){ die(); }
  $result = array('msg' => 'my_module_example - hello from php','valid' => true);
  drupal_json($result);
  exit();
}

Put this Javascript code inside your module's javascript file, e.g. sites/all/modules/my_module/my_module.js

if (Drupal.jsEnabled) {
$(document).ready(function () {
		
			
  // ajax return
  var my_module_example = function (data) {
    alert(data.msg);
  }
  		
  // ajax call
  $.ajax({
    type: 'POST',
    url: Drupal.settings.basePath + 'my_module/example',
    dataType: 'json',
    success: my_module_example,
   data: 'js=1&my_module_post_data=2012'
  });

});
}

Don't forget to clear Drupal's menu cache and grant permissions to your users to be able to access the menu callback url.

Comments

hi..i'm new developer in drupal...sorry for my question..

I have my function 

 

drupal_add_js(drupal_get_path("module","test") . "/tweets.js");

function tst_perm () {
  return array(
    'access test via ajax',
  );
}

function test_menu () {
	
  $items = array();
	
  $items['test/get_tweets'] = array(
    'page callback' => 'test_get_tweets',
    'access arguments' => array('access my_module via ajax'),
    'type' => MENU_CALLBACK,
  );
	
  return $items;
}

function test_get_tweets () {
  //received data json from api twitter with curl
  //......
  //......
  
  drupal_json($result);
  exit();
}

i want use my tweets in client side..to get them with ajax in javascript I'll have to 'do the following or wrong? 

if (Drupal.jsEnabled) {
$(document).ready(function () {
  	 $.getJSON(Drupal.settings.basePath + 'test/get_tweets',function(){
                  handle data} ....
	

thanks..

 

tyler's picture

This looks like it would work well. I'm not too familiar with the .getJSON function, but I'm sure it works well. I'd recommend not using the drupal_add_js call to add your script. Instead use your module.info file to make Drupal aware of your javascript file.

https://drupal.org/node/304255

scripts[] = my_script.js

 I already inserted the script in mymodule.info but it would seem that the url does not work in $.getJSON OR AJAX