By kuldeepkaundal on
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
Outside of Drupal
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
No,that file is not outside
No,that file is not outside drupal, its in the themes directory itself, i am calling this file inside my custom page.
Drupal framework, not directory
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.
---------------------------------
Steven Wright
Slalom
Please expand
I've created my module but don't understand the part
I've created my menu hook_item but don't understand the respond to url, my ajax currently looks like this
Would someone kindly shed some light.
Thanks