Need a standalone page that has access to all Drupal modules
.kuma - October 30, 2009 - 20:50
I have a page that performs an AJAX call to a PHP function. The function is on a Drupal page so it has access to all the Drupal modules. Only problem is the AJAX responseText returns the script result as well as the entire page.
How can I make a page that has access to all modules and can be called by AJAX? I need to call a function in one of the modules I have installed. If I make a standalone PHP file containing the function, it won't have access to the functions in the modules...
Thanks!

I am guessing your function
I am guessing your function returns the script results. Change so it prints the results instead.
Thanks for the reply. I even
Thanks for the reply.
I even tried it without the function entirely; the rest of the page is still returned.
On the AJAX side, I'm using responseText:
result = xmlhttp_obj.responseText;document.getElementById('server_results').innerHTML = result;
Is there an alternative to using responseText?
Whats you PHP function look
Whats you PHP function look like?
Thanks for the
Thanks for the reply.
Something like this:
function add_points($the_user, $num_points) {
$params = array (
'uid' => $the_user,
'points' => $num_points,
);
userpoints_userpointsapi($params);
}
To make it work the way you
To make it work the way you want, add_points() needs to print something.
So after the call to userpoints_userpointsapi() you need something like
print "Your points have been added";Sorry, that was just example
Sorry, that was just example code. I would do something like you mentioned, but in my testing, even with no function call, responseText returns the whole page. I guess that makes sense - I put the function on a standard Druapl page, so all the scripts, etc. run when that page is called.
Should I move the function to a separate, standalone file (outside of Drupal, but I'll still need access to the modules...how to do this?) or is there a better way to return data from the function page to the calling page besides using responseText.
Thanks!
If you have a path defined
If you have a path defined in hook_menu() that uses the function as a callback and the javascript uses that path to invoke the function and the function prints (not returns) its output and it returns nothing it should only return the html printed by the function.
Lets assume the path is 'printme', you can test the path manually in the browser and it should only show the printed results (not the whole page). This assumes you are not explicitly calling
theme('page', ....);I'm not sure how to define a
I'm not sure how to define a path in hook_menu...
Here's the function that's on a page (content type: Page):
<?php
$user_id = $_POST['User'];
// Function from Userpoints module API
$params = array (
'uid' => $user_id,
'points' => $_POST['Points'],
);
userpoints_userpointsapi($params);
// Another function from Userpoints module API
$new_points = userpoints_get_current_points($the_user_id);
print "Your total points are now $new_points";
?>
On the page with the AJAX that calls this function, I do an innerHTML replace like this:
result = xmlhttp_obj.responseText;document.getElementById('results').innerHTML = result;
But the WHOLE page is returned, not just this portion:
print "Your total points are now $new_points";If I go to the page with the function, I see the standard header and footer. If I view the source on that page, I see the standard header with all scripts, etc. loaded...
Thanks so much for your help thus far.
OK, I would not have guessed
OK, I would not have guessed that. You really need a module, see http://drupal.org/developing/modules
Thanks for your help!
Thanks for your help!