hi,

i am trying o call a php file through ajax using following code inside a page in my themes directory:

$(document).ready(function(){ $("#selectionresult").hide(); $("#country").change( function() { $("#selectionresult").hide(); $("#result").html('Retrieving ...'); $.ajax({ type: "POST", data: "data=" + $(this).val(), url: " echo path_to_theme(); /get_states.php", success: function(msg){ if (msg != ''){ $("#selectionresult").html(msg).show(); $("#result").html(''); } else{ $("#result").html('No item result'); } } }); }); });

The get_states.php file makes use of db_query and other drupal functions. but i keep getting Fatal Error: call to undefined function db_query() error,plz suggest me a way to resolve this issue.

Thanks,
Kul.

Comments

stevenc’s picture

Your "get_states.php" file is outside of Drupal, therefore it doesn't have access to any of Drupal's functions. Basically, you can't just add PHP files to a Drupal framework and call them directly. The processing code must exist within another module.

What I think you want to do here is create a Menu item in your module, then have the menu callback refer to whatever code you want it to implement.

---------------------------------
Steven Wright

Slalom

kuldeepkaundal’s picture

No,that file is not outside drupal, its in the themes directory itself, i am calling this file inside my custom page.

stevenc’s picture

By "outside" I meant that the file was outside of the Drupal framework.

Drupal has no awareness of your file, and your file has no awareness of Drupal. You can't just drop a PHP file into a directory and have it automatically become a part of the application.

Migrate the code from your PHP file into a Drupal module (create a custom one for your site) and then create a menu item (via hook_menu) to respond to the URL. This will give your code proper access to the framework.

/**
 * Implementation of hook_menu().
 */
function MYTHEME_menu() {
	$items = array();
	
	// get_states processor 
	$items['get_states'] = array(
		'title' => t('Get a list of states'),
		'description' => t('Provides a list of states'),
		'page callback' => 'MYTHEME_get_states',
		'access arguments' => array('access content'),
		'type' => MENU_NORMAL_ITEM,
	);

    return $items;
}

function MYTHEME_get_states() {
    ...code goes here...
}

---------------------------------
Steven Wright

Slalom

billshankley’s picture

I've created my module but don't understand the part

create a menu item (via hook_menu) to respond to the URL

I've created my menu hook_item but don't understand the respond to url, my ajax currently looks like this

Drupal.behaviors.myModuleBehavior = function (context) {

function requestTransResults() {
	
	$.ajax({
		type: "POST",
		url: "themes/garland/includes/results.php",
		data: 	"serialNo=" + document.getElementById("serialNo").value,
		success: function(html){
			$("#output").html(html);
		
		}
	});
}

}

Would someone kindly shed some light.

Thanks