This may be a basic question, but I am unable to find the answer.

Here is my problem.

I have page that displays results from a DB query.
I have two links per row "Complete" and "Delete".
How can I call a function to update the status of the DB by clicking on one of the links?



mradcliffe’s picture

You basically want to create a menu item of type MENU_CALLBACK. Something like this in hook_menu...

function my_module_menu() {
  $items['path/to/%/complete'] = array(   // assuming you want to make this dynamic
    'title' => 'Complete',
    'page callback' => 'my_module_complete_function',
    'page arguments' => array(2),   // references %, i'm assuming this is all dynamic
    'access arguments' => array('access content'),  // don't do this, whatever permission you want goes here
    'type' => MENU_CALLBACK
  $items['path/to/%/delete'] = array(
    'title' => 'Delete',
    'page callback' => 'my_module_delete_function',
    'page arguments' => array(2),
    'access arguments' => array('access content'), // whatever see above
    'type' => MENU_CALLBACK   
  return $items;

// ... skip ahead to where you put your function
function my_module_delete_function($identifier_for_what_percent_is) {
  return 'blah blah blah blah blah'; //text to return to display

If you wanted to go about it a different way you could make the path something like path/to/%/% and pass in array(2,3). And then the third option would be like an $op variable for one function that you call instead of two. I like to split my functions up personally.

Edit: had this open for like an hour before posting.

athom09’s picture

is there not some way to call PHP from java script?

athom09’s picture

I think that I understand what you are saying. However, I need a little further clarification.
I need two params, an id and a status.

1. When I build my link should the path portion look like this "path/%/%/update" or this "path/1/A/update" ?
2. In the menu_hook how do I pass the dynamic params to 'page arguments'? It is my understanding that the menu_hook is defined before the links are created if the links are dynamic.
Is the Array(2) passing the value 2 into the function or is it refering to index value 2 in path where path=0 to=1 %=2?


Tokoh’s picture

Your explanation of how to get from a link to a function helped enormously but it ends with a "return 'blah blah blah blah blah';".
This return places you on a new page with the title "Complete" or "Delete" and a line below with the return text, in this case, "blah blah blah blah blah".

What I'd like to do at the end of the function "my_module_delete_function" rather than "return 'blah blah blah blah blah';", is to reload the page that had the initial link on it.

How do I do that?

jcsnyder’s picture

Sorry, did you figure this out Tokoh? I'm looking to implement something similar.


alpesh_trivedi’s picture

I am also looking for this answer, please reply if you got solution ....

jimafisk’s picture

I think you may be looking for drupal_goto('your/path/here')