PHP文件中数组中的PHP值在通过Ajax调用该文件时显示错误!

PHP文件中数组中的PHP值在通过Ajax调用该文件时显示错误!,php,ajax,Php,Ajax,我使用以下方法调用php文件 index.php: <div class="pic"> <a id="showcase1" href="showcase/showcase1.php"><img src="images/showcase1t.png"/></a> </div><!-- .pic --> 从语言文件(lang.en.php)调用数组中的值: 如果我直接打开showcase

我使用以下方法调用php文件

index.php:

<div class="pic">
            <a id="showcase1" href="showcase/showcase1.php"><img src="images/showcase1t.png"/></a>
        </div><!-- .pic -->
从语言文件(
lang.en.php
)调用数组中的值:

如果我直接打开
showcase/showcase1.php
,数组会完美地显示语言文件中的值,但是当使用fancybox从锚链接调用它时,它会显示:

致命错误:调用undefined 中的函数l() D:\wamp\www\projects\alexchen\alexchen2.6\showcase\showcase1.php 在线3

有什么建议吗

编辑
l
是语言文件中的一个数组,但我稍后会将其用作函数(请参见本地化.php末尾)。

我不确定,但我要做的第一件事是从showcase1.php中的本地化.php复制所有代码。如果AJAX调用以这种方式工作,那么您可以确定fancybox调用php的方式存在问题。然后我会使用Firebug或live http headers Firefox扩展来检查showcase1.php的调用方式。

添加一个错误报告(E\u ALL);在那里。我怀疑您在尝试包含localization.php时遇到了另一个错误,但由于某些原因,它没有显示出来。您可能还必须在php.ini中或使用ini_set()启用display_errors


使用工具找出直接调用脚本和使用AJAX调用脚本的区别。我推荐Fiddler。

您应该在完成这些调用后使用require\u。我敢说它找不到localization.php

Include仅在文件不存在时发出警告,而require将发出致命错误。如果没有它程序无法正常工作,请始终使用require over include


相对而言,AJAX版本是从不同的路径调用的吗?

我发现了问题所在。当在
showcase/showcase1.php
中调用
localization.php
langauages/lang.en.php
时,php代码无法归档这些文件。我将所有showcase文件放在根目录中,现在一切正常。感谢他们的回答,他们帮我找到了答案

我猜是路径问题。从Ajax运行时,工作目录是什么?“../localization.php”是否指向本例中的文件?好吧,我不知道为什么会投反对票,但我确信,正如Colin Fine所说,由于从ajax调用php的方式,include路径存在问题,您肯定会这么做。检查包含路径是否有问题的另一种方法是在包含文件时使用完整路径,而不是相对路径。
<?php include_once '../localization.php'; ?>
<div id="inline">
    <img src="images/showcase2.png"/>
    <?php echo l('inline_p'); ?>
    <a href="http://studyatbest.com"><?php echo l('inline_a'); ?></a>
</div>
'inline_p' => ' <p><strong>Previous</strong> = Left Arrow Key</p>
                <p><strong>Next</strong> = Right Arrow Key</p>
                <p><strong>Close</strong> = Esc</p>',
'inline_a' => 'Visit',
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang'])) {
    $lang = $_GET['lang'];

    // register the session and set the cookie
    $_SESSION['lang'] = $lang;
    setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang'])) {
    $lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang'])) {
    $lang = $_COOKIE['lang'];
}
else {
    $lang = 'en';
}

// use appropiate lang.xx.php file according to the value of the $lang
switch ($lang) {
case 'en':
    $lang_file = 'lang.en.php';
    break;

case 'es':
    $lang_file = 'lang.es.php';
    break;

case 'zh-tw':
    $lang_file = 'lang.zh-tw.php';
    break;

case 'zh-cn':
    $lang_file = 'lang.zh-cn.php';
    break;

default:
    $lang_file = 'lang.en.php';
}

//translation helper function
function l($localization) {
    global $lang;
    return $lang[$localization];
}

    include_once 'languages/'.$lang_file;
?>