【问题标题】:PHP file loaded with jQuery.ajax() can't access loaded functions用 jQuery.ajax() 加载的 PHP 文件无法访问加载的函数
【发布时间】:2012-06-18 03:08:40
【问题描述】:

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

我正在从一个线性 PHP+JavaScript CMS 过渡到一个面向对象的 PHP 模型,其中包含一些花里胡哨的 jQuery 和 Ajax。我正在尝试使用以下设置集中尽可能多的代码:

index.php

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

functions.js

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

/inc/文件夹

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

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

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

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

过去,我只使用 PHP 和 POST/GET 将表单加载到位,这显然将索引标题、所需文件和新内容重新加载为一个有凝聚力的系列。那么如何结合 Ajax 以及它不重新加载的情况获得相同的结果呢?甚至可能吗?我是不是走错了路?

一些注意事项:

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

提前致谢!

index.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>

functions.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,它让用户有机会为他们的站点创建一个页面。为了确定用户的新页面是否是另一个页面的子页面(稍后在下面的表单中称为父页面),创建一个&lt;SELECT&gt;并通过一个PHP函数填充&lt;OPTION&gt;s(位于 functions.php 文件中)调用 getPagesAsSelect()。

<?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() 也会阻止新内容调用它需要的函数。

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

【问题讨论】:

  • 真的,你有例子吗?
  • 无论你解释得多么详细,如果你不显示一些代码也无助于你解决问题。
  • 在您输入所有内容的时间里,您可以输入您的代码的简化版本,我们称之为“示例代码”,向我们展示您的文字墙正在跳舞试图定义。

标签: php jquery require-once


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多