加载jQuery.ajax()的PHP文件可以';t访问加载的函数

加载jQuery.ajax()的PHP文件可以';t访问加载的函数,php,jquery,require-once,Php,Jquery,Require Once,如果您不想阅读设置的详细信息,请跳到最后两段。 我正在从线性PHP+JavaScriptCMS过渡到一个面向对象的PHP模型,其中包含一些简单的jQuery和Ajax。我正试图通过以下设置尽可能集中代码: index.php 调用(通过require_once())setup.php(轻松访问常用变量(如目录)、常量、首选项、打开和关闭调试等)、actions.php(jQuery.ajax()用于确定将数据发送到哪个函数或对象的文件)、functions.php(杂项函数)、objects.

如果您不想阅读设置的详细信息,请跳到最后两段。

我正在从线性PHP+JavaScriptCMS过渡到一个面向对象的PHP模型,其中包含一些简单的jQuery和Ajax。我正试图通过以下设置尽可能集中代码:

index.php

  • 调用(通过require_once())setup.php(轻松访问常用变量(如目录)、常量、首选项、打开和关闭调试等)、actions.php(jQuery.ajax()用于确定将数据发送到哪个函数或对象的文件)、functions.php(杂项函数)、objects.php(通常用于准备MySQL数据库的传入/传出数据的对象)
  • 调用functions.js、jQuery(来自谷歌)、tiny_mce和fancybox(后两个可能不是问题的变量,但我想我还是要提到它们)
  • 包括div#content,以便jQuery.ajax()有一个加载内容的位置
functions.js

  • 包括一个脚本,用于通过$(#content.html)将内容加载到index.php的div#content中
  • 包括用于准备表单内容的脚本,使用.ajax()将数据发送到actions.php,接受来自服务器的成功或错误消息,并向用户显示消息
  • 包括一个doc.ready函数,该函数将.live('submit')方法添加到表单中
/公司/文件夹

  • 包括PHP文件,主要是表单,用于通过.ajax()加载到索引的div#content中
大多数情况下一切正常。不起作用的部分是加载到索引的div#content中的表单无法访问索引已经加载的各种PHP文件(即setup.PHP和functions.PHP)。这些表单需要临时函数来填充自己

例如,我有一个“表单”,显示网站中的所有页面,以便用户可以编辑或删除它们

目前表单无法访问getPages()函数,因为.ajax()加载的文件无法识别index.php加载的functions.php。我可以通过在/inc/文件夹中每个文件的顶部添加require_once('functions.php');来解决这一问题,但我不希望这样。我希望我的表单只是表单

在过去,我只使用PHP和POST/GET将表单加载到位,这显然将索引头、所需文件和新内容作为一个内聚的系列重新加载。那么,我如何结合Ajax获得相同的结果,并且不需要重新加载?这可能吗?我的做法是否错误

一些注意事项:

  • 我更喜欢jQuery、JavaScript或PHP解决方案,如果有的话。我所做的大部分工作对我来说都是新的,因此添加另一个库可能会让我大吃一惊……但如果这是唯一的方法……我会做我必须做的事
  • 虽然我很乐意根据需要复制/粘贴一些代码,但没有一个文件是在线可用的;它只是在我的XAMPP开发服务器上
  • 因为这些文件都在我的服务器上,所以显然没有跨域的bidness
提前谢谢

php(此文件中的所有内容似乎都正常工作)

一个示例页面:add_page.php,它让用户有机会为他们的站点创建一个页面。为了确定用户的新页面是否是另一个页面的子页面(后一个页面在下面的表单中称为父页面),将创建一个
,并通过php函数填充
(位于functions.php文件中)名为getPagesAsSelect()


小结:您希望在php文件中使用函数,而不必在该文件中有
require\u一次
及其定义。2种解决方案:

  • 不要直接加载该文件,而是加载函数文件并包含所需文件的一个简单文件(例如用`/pagecontent.php?page=pagename.php调用)(不要将名称列入白名单,不要包含随机文件)
  • 创建一个
    auto_prepend
    设置

  • 您的“函数”在ajax调用中不可访问,因为它们是与原始(index.php)完全不同的请求并且对以前的请求一无所知。没有解决方法。如果您需要访问其他文件中定义的那些函数,则必须包含/要求这些文件。如果您不想在文件中使用
    包含
    /
    要求
    ,则需要重新考虑您的实现。

    这不是解决方法,而是PHP的工作方式。请注意因为PHP是服务器端,您无法连接到在其他服务器调用中加载的函数。它们不连接。PHP就是为此而设计的。使用它。

    现在真的吗,您有一个示例吗?无论您如何详细地解释它,如果您不显示一些代码,它都无法帮助您解决问题。在您键入所有这些代码的时间内,您可以我已经输入了你的代码的简化版本,让我们称之为“示例代码”,向我们展示你的文本墙在试图定义什么。
    <?php
    require_once('setup.php');
    require_once('functions.php'); //functions.php calls to objects.php
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    ...
    <link href="styles/styles.css" type="text/css" media="screen" rel="stylesheet" />
    <link href="fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
    ...
    <script src="js/functions.js"></script>
    </head>
    <body>
    <div id="pagewrapper">
    <div id="header">unimportant header content</div><!-- /header -->
    
    <div id="nav_area">
      <ul id="nav">
        <li class="parent">Pages</li>
        <ul class="children">
          <li class="child" onclick="navto('inc/add_page.php');">Add a Page</li>
        </ul>
      </ul>
    </div><!-- mana_nav_area -->
    
    <div id="content"><!-- This is the DIV where the forms will appear -->
    <?php
    echo getPagesAsSelect(); //the function works here, although it doesn't make any sense to put it here. It's only here for illustrative purposes. 
    ?>
    </div><!-- mana_content -->
    </div><!-- pagewrapper -->
    </body>
    </html>
    
    jQuery(document).ready(function(){
    // called by index.php, this function adds an event handler to our example form
        ...
        $("#add_page_form").live("submit", function () { add_page(); return false; });
            ...
    });
    
    function navto(destination) {
    // use JQuery's ajax method to control main navigation
        var request = $.ajax({
            url: destination
        });
    
        request.done(function(msg) {
            $("#content").html( msg );
        });
    
        request.fail(function(jqXHR, textStatus) {
            alert( "Request failed: " + textStatus );
        });
    }
    
    function add_page() {
    // this function grabs the data from the add_page_form and sends it to actions.php
    // actions.php determines what function or object to send it to
    // JS-based security is not an issue at this time
        var serialized = $('#add_page_form').serialize();
        var dataString = 'action=add_page&' + serialized;
        var destination = 'actions.php';
    
        var jqXHR = $.ajax({
            type: "GET",
            url: destination,
            data: dataString,
            cache: true,
            error: function(response) {
                $('msg').append(response);
            },
            success: function(response) {
                $('#msg').append(response);
            }
        });
        return false;
    }
    
    <?php
    // if I leave this code in place, it works
    require_once('../functions.php'); //(you may recall that this file is in an /inc folder)
    // this require_once() is the one I'd like to remove if possible
    ?>
    <fieldset id="fieldset">
    <legend>Add a Page</legend>
    <form id="add_page_form" name="add_page" action="" method="get">
    
    <label for="name">Page Name:</label> 
        <input type="text" id="name" name="name" value="" /><br />
    <label for="parent">Parent:</label>
        <select id="parent" name="parent">
            <option value="0">No parent</option>
        <?php
                echo getPagesAsSelect();
                // getPagesAsSelect() queries the database and returns eligible
                // pages as a series of <option value="pageID">pageName</option>
                // Unless functions.php is loaded on this page, this call fails
                // and the page stops loading here
            ?>
            </select>
        <br />
    <input id="submit_add_page" type="submit" name="add_page" value="Submit" class="save" /><input type="reset" value="Cancel" class="cancel" />
    </form>
    <div id="msg"><!-- where success or error messages show upon completion --></div>
    </fieldset>