简体   繁体   English

drupal undefined function static html with post to php

[英]drupal undefined function static html with post to php

I have placed my own php file in /sites/all/modules/<myfolder> and call it via $.post(<myphppage>) from a static html page (javascript function) also in /sites/all/modules/<myfolder>. 我已经将自己的php文件放在/ sites / all / modules / <myfolder>中,并通过$.post(<myphppage>)从静态html页面(javascript函数)也在/ sites / all / modules / <myfolder中调用它>。

However, the php does not appear to be executing as expected, but is getting called (access logs in Apache HTTPD show it is post'ed to). 但是,php似乎没有按预期执行,而是被调用(Apache HTTPD中的访问日志显示它已发布到)。

If I try to manually request the same php page, I receive this error: 如果我尝试手动请求相同的php页面,则会收到此错误:

Fatal error: Call to undefined function db_insert() in /full/path/to/sites/modules/<myfolder>/<myphppage> on line x. 致命错误:在第x行的/ full / path / to / sites / modules / <myfolder> / <myphppage>中调用未定义的函数db_insert()。

The echo statement I have in the php page is outputted properly above this error, and simply uses $_GET['paramname'] . 我在php页面中拥有的echo语句会在此错误上方正确输出,并仅使用$_GET['paramname'] (And _POST, changed for testing direct request) to print a few query string parameters for debugging). (和_POST,更改为测试直接请求)以打印一些查询字符串参数以进行调试)。

Also, when I added a call to watchdog , I receive: 另外,当我向watchdog添加呼叫时,我收到:

Fatal error: Call to undefined function watchdog() in /full/path/to/sites/modules/<myfolder>/<myphppage> on line x. 致命错误:在x的/ full / path / to / sites / modules / <myfolder> / <myphppage>中调用未定义的函数watchdog()。

Is it not possible to access the PHP page directly? 无法直接访问PHP页面吗? Or is there a Drupal library I need to import? 还是我需要导入一个Drupal库? Or am I just plain missing something else with how Drupal works? 还是我只是简单地缺少Drupal的工作方式?

You are getting those errors about undefined functions because your PHP file is independent from Drupal and it is not bootstrapping it. 您会收到有关未定义函数的那些错误,因为您的PHP文件独立于Drupal且没有自举。

The Drupal way of doing what you are trying to do is to create a module (see Creating Drupal 7.x modules , and Creating Drupal 6.x modules ) that defines a URL it handles with hook_menu() ; Drupal要做的事情是创建一个模块(请参阅创建Drupal 7.x模块创建Drupal 6.x模块 ),该模块定义它使用hook_menu()处理的URL; that URL is then used with $.post() . 然后将该URL与$.post()

As example of the way of bootstrapping Drupal, see the content of the index.php file that comes with every Drupal installation. 作为引导Drupal的方法的示例,请参阅每个Drupal安装随附的index.php文件的内容。 (The following code is the one used from Drupal 7.) (以下代码是Drupal 7中使用的代码。)

/**
 * Root directory of Drupal installation.
 */
define('DRUPAL_ROOT', getcwd());

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

The code changes from Drupal 6 to Drupal 7, but drupal_bootstrap() is defined in both Drupal versions. 代码从Drupal 6更改为Drupal 7,但是在两个Drupal版本中都定义了drupal_bootstrap()

The correct way is to create a module, anyway. 无论如何,正确的方法是创建一个模块。 For a list of reasons why a module should use a PHP file that bootstraps Drupal, see Are there cases where a third-party module would need to use its own file similar to xmlrp.php, cron.php, or authenticate.php? 有关模块为什么要使用引导Drupal的PHP文件的原因的清单,请参阅在某些情况下第三方模块需要使用自己的类似于xmlrp.php,cron.php或authenticate.php的文件吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM