views:

304

answers:

3

I want a link that when you click on it, it will call a simple ajax query which updates a database entry. Normally this would be easy, but because authentication needs to happen before the query is executed, I need to be able to execute the Ajax Query AS THE CURRENTLY LOGGED IN USER. I.e. I need access to the $user variable and I don't want to create a new connection.

Does Drupal have native functions that can do this? Is there something I can use to do this?

+4  A: 

You're going to want to create a module to handle this. Then, whatever function you call through the callback will have complete awareness of the logged in user.

mabwi
I realised this after posting the question. Thanks. It confirmed what I thought.
RD
A: 

Either create a menu item in a module implementing hook_menu() and specify a callback to a function handling this request - or use the Services module and create a new service module.

VoxPelli
+1  A: 

Keep in mind that if someone makes a link that goes to your AJAX destination on their website, and can trick people into clicking it (or, for that matter, makes that link the src on an img tag), then weird things can happen to people's data without them meaning to.

I suggest the use of a token and checking that the token submitted as part of the link matches something to do with the user. Check drupal_get_token() for a good way to generate one.

John Fiala