0

I would like to pass data from Javascript to PHP using Drupal 7.14. To this end, I'm trying to follow this SO post: https://stackoverflow.com/questions/4834132/how-to-pass-jquery-variable-to-drupal-ajax.

However, |hook_menu()` is not working as expected.

function example_module_menu() {
  $items = array();

  $items['example_callback/%'] = array(
    'title' => 'My Custom Callback', 
    'description' => 'Description', 
    'page callback' => 'my_custom_php_function', 
    'page arguments' => array(1), // fill this
    // 'access arguments' => array('access content'), 
    'access callback' => true,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Function that gets called from JQuery asynchronously. 
 */
function my_custom_php_function($argument) {
  // Do something with $argument...
  echo $argument;   
}

In my understanding, going to http://mywebsite.com/example_callback should call my_custom_php_function. Instead, I get:

The requested URL /example_callback was not found on this server.

Are there other steps necessary to write a simple menu callback?

2
  • Menu items are cached, so make sure you clear the cache by going to Administer -> Performance.
    – Brian
    Jun 19, 2012 at 17:14
  • thanks for the suggestion, but it didn't work :/
    – Blaine
    Jun 19, 2012 at 17:41

1 Answer 1

3

Your URL is expecting an argument so your URL would have to be http://mywebsite.com/example_callback/something. I wouldn't bother with the extra % in the path though.

So you should be doing something like and it will still pick up the arguments:

function example_module_menu() {
  $items = array();

  $items['example_callback'] = array(
    'title' => 'My Custom Callback',
    'page callback' => 'my_custom_php_function',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
 );

 return $items;
}

/**
* Function that gets called from JQuery asynchronously. 
*/
function my_custom_php_function() {
  if (arg(1)) {
    // this means there is an argument
    // Do something with $argument...
  }
  return '';
}
10
  • Try the above again, but add a return; Jun 19, 2012 at 17:57
  • Still receiving a 404 error when I navigate to myexample.com/example_callback/something. Two follow-up questions: 1) Am I supposed to write something else for the above hook to work, e.g. a .install file? 2) Do I have to create a new page, e.g. example_callback.php?
    – Blaine
    Jun 19, 2012 at 17:59
  • return $items; does not fix the problem.
    – Blaine
    Jun 19, 2012 at 18:00
  • No to both questions, you just need the menu item in hook_menu() and the callback function. I meant you should add a return in the page callback. Jun 19, 2012 at 18:03
  • I've just tried the above at it works perfectly my end. Are you placing this in a module called 'example_module'? Jun 19, 2012 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.