Been playing around with using ajax in particular JAH

trying to use the principles of using a load.php file to load the relevant node but not having a lot of success in getting it to work, im generating an error such as

Fatal error: Call to undefined function: node_load() in C:……\load.php on line 5

The php code looks as follows

 
require_once 'includes/bootstrap.inc';
require_once 'includes/common.inc';
 
$MrNode = node_load(array("nid" => 1));
print $MrNode->body;
 

I cant see why other than the fact that I am missing something really silly
Any one any ideas how i can call the node_load function in my php file

Comments

nevets’s picture

All the includes do is pull in code, they do not execute anything. If you look at index.php you will see

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

it is the call to drupal_bootstrap() that sets things up and makes calls to other parts of Drupal possible.

Note you can also do what you want with a menu callback.

somes’s picture

cheers for pointing that out it was the missing piece that being the

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

sorry nevets but how will the menu callback helpme/work

do you know of any method of sending out a query to a php file and sending back a variable to the sender

nevets’s picture

With a custom module using the menu hook you can define a path that results in a function being called. So for example if your module is called mymodule you might do something like

function mymodule_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array('path' => 'mymodule/fetch',
      'callback' => 'mymodule_fetch',
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK);
  }
  return $items;
} 

function mymodule_fetch() {
  $value = 20;

  print $value;
}

And then you can use the URL http://www.example.com/q=mymodule/fetch. If you need to pass data you can do it my adding the data to the url, like this http://www.example.com/q=mymodule/fetch/data1/data2 and in this case the declaration for the function would be function mymodule_fetch($arg1, $arg2)

somes’s picture

tks for answering that menu hook just might solve my problem with passing variables back..needs some experimenting... I'm trying to get the jQuery to pass data via AJAX there seems to be a method that uses xml but this isnt really ideal as the node_load pass will need to be parse to XML before being sent back to the jQuery caller

so I'll have a go at doing it via variable using that menu hook

tks again

vood002’s picture

I'm implementing this method and it seems to be working well. I'd figured I would be able to find a 'cleaner' method to load node content via ajax but i'm currently just doing a jq load() to a menu callback.

Just out of curiosity...this doesn't seem that clean...having a menu callback function print a response rather than properly returning something. Is this still a preferred method of ajax loading content in drupal 7?