加载 jQuery.ajax() 的 PHP 文件无法访问加载的函数


PHP file loaded with jQuery.ajax() can't access loaded functions

如果您

懒得阅读设置的详细信息,请跳到最后两段...

我正在从线性 PHP+JavaScript CMS 过渡到面向对象的 PHP 模型,就像 jQuery 和 Ajax 一样。我正在尝试使用以下设置集中尽可能多的代码:

索引.php

  • 调用(通过require_once())设置.php(轻松访问公共变量(如目录),常量,首选项,打开和关闭调试等),操作.php(jQuery.ajax()用于确定将数据发送到哪个函数或对象的文件),函数.php(杂项函数),对象.php(通常用于准备传入/传出MySQL数据库的数据的对象)
  • 调用函数.js,jQuery(来自Google),tiny_mce和fancybox(最后两个可能不是问题中的变量,但我想无论如何我都会提到它们)
  • 包含div#content,以便 jQuery.ajax() 有一个加载内容的地方

函数.js

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

/inc/文件夹

  • 包括PHP文件,主要是表单,用于通过.ajax()加载到索引的div#content中

大多数一切都很好。不起作用的部分是加载到索引的div#content 中的表单无法访问索引已经加载的各种 PHP 文件(即 setup.php 和 functions.php)。这些形式需要偶尔的功能才能填充自己。

例如,我有一个"表单",显示网站中的所有页面,以便用户可以编辑或删除它们。在我的旧CMS模型中,我会将该函数(我们称之为getPages())存储在函数中.php以便页面可以简单地回显getPages();

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

过去,我只用PHP和POST/GET加载表单,这显然将索引头,所需文件和新内容作为一个有凝聚力的家族重新加载。那么我怎样才能在Ajax和它没有重新加载的情况下获得相同的结果呢?甚至可能吗?我这样做的方式是错误的吗?

一些注意事项:

  • 我更喜欢jQuery,JavaScript或PHP解决方案,如果有的话。我正在做的大部分事情对我来说已经是新的了,所以添加另一个库可能会让我的头爆炸。...但如果这是唯一的方法...我会做我必须做的事情。
  • 虽然我很乐意根据需要复制/粘贴一些代码,但没有一个文件可以在线获得;它只是在我的XAMPP开发服务器上。
  • 由于这些文件都在我的服务器上,因此显然没有跨域出价需要考虑

提前感谢!

索引.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>

函数.js(此文件中的所有内容似乎都可以正常工作)...

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;
}

示例页面:add_page.php,它使用户有机会为其网站创建页面。为了确定用户的新页面是否是另一个页面的子页面(后者在下面的表格中称为父页面),通过一个名为getPagesAsSelect()的PHP函数(位于函数.php文件中)创建一个<SELECT>并填充<OPTION> s。

<?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>

总结一下:.ajax() 将上述add_page.php拉入 index.php 的div#content 中。即使 index.php 加载了 functions.php 文件,使用 .ajax() 也会阻止新内容调用它所需的函数。

虽然我不怀疑一切都在正常工作,但我不喜欢我的解决方法,即需要函数.php每个文件都由 .ajax() 加载。我正在寻找替代方案,如果有的话。

小总结:你想在 php 文件中使用一个函数,而不必在那里require_once文件及其定义。 2 种解决方案:

  1. 不要直接加载该文件,而是 1 个简单的文件,加载函数文件,然后包含您想要的文件(例如调用 '/pagecontent.php?page=pagename.php(将名称列入白名单,不要包含随机文件)。
  2. 创建auto_prepend设置。

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

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