So basically, I'm creating a module that retrieves output from a php script. I'm using ajax to do this. I've got the module to work when I create it outside of Drupal, but when I pretty much copy the same code in and make changes it doesn't work, I either get 404 errors or no output was so ever. Javascript and JQuery work fine as I've done basic alerts and passed information through my textfield and alerted out the information on 'keyup' events, but the issue only comes up when I incorporate Ajax into the equation. I'm using functions like $.get() and $.ajax, but niether seem to work, even when I try to retrieve a simple echo "something" into my php script. I was wondering which drupal hooks would do the trick or how I get it to work using jQuery. Any help would be much appreciated.
Thanks,
Zee
Comments
I'm having a look at doing
I'm having a look at doing some AJAX coding just now now having never done any before,
This page might be of use to you, these are the instructions I'm gonna try following at least
http://drupal.org/node/42562
" In Drupal, AJAX functionality is provided through functions in the Javascript file
drupal.js"Sorry that last page is a
Sorry that last page is a drupal4 tutorial
This page appears to be the D5/D6 version of an intro to using AJAX with Drupal
http://drupal.org/node/305747
Also there are lot's of modules that use AJAX that might be worth having a look at their code
http://drupal.org/project/modules?filters=drupal_core%3A87&text=ajax
I've had a chance to re-write
I've had a chance to re-write a version of the module. The key is to use Drupals built in menu system to perform url-maps. So for example, I'd use hook_menu and create something like this:
hook_menu()
Essentially this just says when I go to example.com/url-of-page/ Drupal is going to call some_function_that_will_process() which would be in your .module
in some_function_that_will_process() you could check the sent data (via get or post - depending on how your ajax query was formatted) and do what you need. Just return the data at the end with drupals drupal_json() function.
Ideally you would also pass your base URL to javascript through Drupals built in JS "framework". You can do this in hook_init().
That basepath is now available to you in your .js file via
So the url attribute of your ajax query could look like this:
Note that I just used console.log which is available in safari/chrome by default and requires Firebug to show up in Firefox