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

benced’s picture

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 "

benced’s picture

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

xangelo’s picture

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:

<?php 
function hook_menu(){
	$items = array();

	$items['url-of-page'] = array(
		'title' => 'Irrelevant for ajax',
		'description' => 'Irrelevant for ajax',
		'page callback' => 'some_function_that_will_process',
		'access arguments' => array('access content'),
		'type' => MENU_CALLBACK,  // This is key
	);
	
	return $items;
}?>

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().

<?php
$path_settings = array(
		'basepath' => base_path(),
	);
	
drupal_add_js(array('whatever' => $path_settings), 'setting');
?>

That basepath is now available to you in your .js file via

<script type="text/javascript">
Drupal.settings.whatever.basepath;
</script>

So the url attribute of your ajax query could look like this:

<script type="text/javascript">
$.ajax({
	type: 'POST',
	url: Drupal.settings.whatever.basepath+'url-of-page/',
	data: {name: 'mydata'}, 
	success: function(data){ console.log(data); },
	failure: function(){ console.log('failed'); },
	dataType: 'json'
});
</script>

Note that I just used console.log which is available in safari/chrome by default and requires Firebug to show up in Firefox