Ajax and hook menu in Drupal
I've cross posted this on the Drupal website as well, I hope this isn't an issue. I find that a very high percentage of my posts there receive zero response, and, although twf has a much smaller user base, those that do come here tend to be a lot more helpful.
I've implemented my Drupal module as follows:
<?php
function simpim_menu() {
$items['simpim/check_messages'] = array(
'page callback' => 'check_messages',
'type' => MENU_CALLBACK,
);
$items['simpim/writemessage'] = array(
'page callback' => 'writemessage',
'type' => MENU_CALLBACK,
);
$items['simpim/get_message'] = array(
'page callback' => 'get_message',
'type' => MENU_CALLBACK,
);
return $items;
}
?>
In my javascript file I have the following:
xmlhttp=$.ajax({
type: 'GET',
url: '/sports/sites/all/modules/simpim/simpim/writemessage/',
data: 'suid='+uid+'&sname='+uname+'&ruid='+partnerID+'&rname='+partner+'&msg='+sendText,
success: function(msg){
alert(msg);
}
});
return false;
});
I'm expecting Drupal to implement the 'writemessage' function as specified in the 'page callback' => 'writemessage', but this does not appear to be happening.
Is my implementation of this okay? Or is my grasp of the concepts of this way off?
my apache access.log has the following lines:
10.0.0.7 - - [17/Aug/2009:09:30:59 +0200] "GET /sports/sites/all/modules/simpim/simpim/check_messages?_=1250494232546&ruid=27 HTTP/1.1" 404 324 "http://bilbo/sports/" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/530.5 (KHTML, like Gecko) Chrome/2.0.172.39 Safari/530.5"
10.0.0.7 - - [17/Aug/2009:09:31:03 +0200] "GET /sports/sites/all/modules/simpim/simpim/writemessage/?suid=1&sname=admin&ruid=27&rname=zed&msg=morning HTTP/1.1" 404 322 "http://bilbo/sports/?q=node/6" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.13) Gecko/2009073022 Firefox/3.0.13 (.NET CLR 3.5.30729)"
In this segment "GET /sports/sites/all/modules/simpim/simpim/writemessage/?suid=1&sname=admin&ruid=27&rname=zed&msg=morning HTTP/1.1" 404
the 404 seems to imply a 'File not found' error, but this may not necessarily be unexpected as the "/sports/sites/all/modules/simpim/simpim/writemessage/" file (or directory) does not exist.
Can anyone assist?
JeevesBond posted this at 08:35 — 18th August 2009.
He has: 3,956 posts
Joined: Jun 2002
I think this post on the Drupal forums is correct (if a little terse).
Drupal does not implement URL callbacks, you'll need to create a PHP function to do that yourself; or use an existing function, like drupal_get_form, for example.
a Padded Cell our articles site!
JeevesBond posted this at 09:02 — 18th August 2009.
He has: 3,956 posts
Joined: Jun 2002
Sorry for the double-post, but I noticed you've received a reply on drupal.org
Have you tried clearing Drupal's cache after creating the code for the menu items? Easiest way, in case you or anyone else who happens to be reading this doesn't know, is to visit the modules page.
I'll admit though: that was a shot in the dark.
a Padded Cell our articles site!
davecoventry posted this at 10:38 — 18th August 2009.
He has: 112 posts
Joined: Jun 2009
JB,
Please don't apologise! It's really frustrating as what I'm attempting to do does appear to be largely correct but for some strange reason it just doesn't work.
My hook_menu callback should trap the call to the URL http://bilbo/sports/simpim/writemessage and run the function 'writemessage'. From what I've read, this should be occurring. But it doesn't. I just get a 404.
I'm wondering if there's something wrong with my Drupal installation and for some reason it's not redirecting the url to the callback.
davecoventry posted this at 11:00 — 18th August 2009.
He has: 112 posts
Joined: Jun 2009
I think there must be something wrong with my Drupal installation.
I've just installed the IM instant messenger module and this doesn't appear to work either.
JeevesBond posted this at 22:39 — 18th August 2009.
He has: 3,956 posts
Joined: Jun 2002
This seems to me to be due to both modules assuming clean URLs are switched on, yet looking at the referrer it seems they're not:
http://bilbo/sports/?q=admin/build/block
What happens if you either, 1) switch on clean URLs, or; 2) change the url to
url: '?q=simpim/writemessage',
(I'm probably wrong about 2 and would expect that not to work, it's where I'd start my investigations though).
a Padded Cell our articles site!
davecoventry posted this at 12:49 — 24th August 2009.
He has: 112 posts
Joined: Jun 2009
JB,
Good spot.
I've tried to put the module on another server which now appears to find the correct path but gives me a 403 error.
davecoventry posted this at 10:23 — 26th August 2009.
He has: 112 posts
Joined: Jun 2009
JB,
I have changed the menu hook as follows:
<?php
function simpim_menu() {
$items['simpim/check_messages'] = array(
'title' => 'Check Message',
'page callback' => 'check_messages',
'access' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function check_messages(){
$messageq=db_query('SELECT sname FROM {im_msg} WHERE ruid=%d',$_GET['ruid']);
$echo='';
if ($row=db_fetch_array($messageq)){
if($echo==''){
$echo=$row['sname'];
}
else{
$echo=$echo.', '.$row['sname'];
}
}
echo $echo;
}
?>
but I am still getting the 403 permission denied error:
192.168.37.43 - - [26/Aug/2009:03:20:48 -0700]
"GET /drupal/simpim/check_messages?_=1251282048675&ruid=1 HTTP/1.1" 403 8518
Can anyone suggest anything?
davecoventry posted this at 12:17 — 26th August 2009.
He has: 112 posts
Joined: Jun 2009
If you visit this forum thread on the drupal site:
http://drupal.org/node/557368
you can read an interesting tussle I'm having with someone called Hiene.
He's trying to tell me where I'm going wrong with as little information as possible, and I'm trying to coax it out of him without actually admitting that I don't know what I'm doing and asking for a walkthrough.
JeevesBond posted this at 13:01 — 26th August 2009.
He has: 3,956 posts
Joined: Jun 2002
Try replacing
simpin_menu
with this:<?php
function simpim_menu() {
$items['simpim/check_messages'] = array(
'title' => 'Check Message',
'page callback' => 'check_messages',
'access arguments' => array('access messages'),
'type' => MENU_CALLBACK,
);
return $items;
}
?>
a Padded Cell our articles site!
davecoventry posted this at 15:35 — 26th August 2009.
He has: 112 posts
Joined: Jun 2009
Hey JB!
It worked!
How is that possible?
What exactly does array('access messages') mean? Surely it's an array with one element which holds a string, but I can't see the relevance of that string.
JeevesBond posted this at 02:29 — 27th August 2009.
He has: 3,956 posts
Joined: Jun 2002
As Heine said, rather tersely, it's a list of the permissions the user has to have before they can access the menu item. If you're testing this as the admin user you'll get a free pass, because you get a free pass for every permission. Try accessing that URL as an anonymous user, or user who doesn't have that permission and you'll see the access denied message.
Does that make sense?
a Padded Cell our articles site!
davecoventry posted this at 06:06 — 27th August 2009.
He has: 112 posts
Joined: Jun 2009
Does that make sense?
Not really.
So the string is actually a space-separated array? Bizarre.
Where do you get a listing for the valid values for this array?
I actually said to Heine that I thought that the 'access arguments' was a list of local parameters to be passed to the callback function and he didn't disabuse me of this. Even when I said that I didn't use it because I was reading the arguments from $_GET mechanism.
Also, I *was* testing from the admin account and it still gave me the 403.
davecoventry posted this at 18:50 — 27th August 2009.
He has: 112 posts
Joined: Jun 2009
JB,
Forgot to say thanks!
No manners, I'm afraid.
JeevesBond posted this at 06:53 — 28th August 2009.
He has: 3,956 posts
Joined: Jun 2002
Forgot to say thanks!
No manners, I'm afraid. :(
No worries. You're welcome.
JeevesBond posted this at 06:52 — 28th August 2009.
He has: 3,956 posts
Joined: Jun 2002
Nope.
It's just an array of arguments to pass to the access function. For example, you could use:
<?php
'access arguments' => array('kiss my arse'),
?>
As long as you define the permission in a hook_perm implementation, you'll be able to see it on the Administer -> User management -> Access control page.
Any strings are valid (see above), I made up the 'access messages' permission. Drupal builds the list of permissions automatically from every module's
hook_perm
, so you really can give it any values you want. Just remember to get Drupal to rebuild its cache by visiting Administer -> Site building -> Modules.Go to: Administer -> User management -> Access control and you'll see your permission there.
It is! Check out user_access. The arguments in the
access arguments
array are handed to theuser_access
function.a Padded Cell our articles site!
davecoventry posted this at 16:49 — 28th August 2009.
He has: 112 posts
Joined: Jun 2009
Bugger!
I thought it was sorted, but it appears to only be sorted for the admin account. Ordinary users are still getting 403.
davecoventry posted this at 21:45 — 28th August 2009.
He has: 112 posts
Joined: Jun 2009
Found it.
It was the hook_perm as you said:
<?php
function simpim_perm(){
return array('access messages');
}
?>
Once I put that into my module, it showed up in the permissions page so I could click 'authenticated use' and it worked.
Sheesh!
I have been struggling with this for 4 weeks!
jontanderson posted this at 12:49 — 25th February 2010.
They have: 1 posts
Joined: Feb 2010
Had the same problem, and have spent countless hours trying to find a solution. The one(s) presented here work! Its awesome!
Thanks so much!
blueeye posted this at 16:56 — 21st March 2010.
They have: 17 posts
Joined: Mar 2010
Same problem arised me last week and I was searching for the issue but can't find it over internet. One of my friend gave me the same solution. Today while reading this post, I got the idea from where my friend got the idea
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.