PHP values inside an arrays in a PHP file are displaying an error when that file is called via Ajax!
I call a php file in 开发者_如何学Gothe following way using Fancybox
index.php:
<div class="pic">
<a id="showcase1" href="showcase/showcase1.php"><img src="images/showcase1t.png"/></a>
</div><!-- .pic -->
inside showcase/showcase1.php
I have the following code:
<?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>
which calls values inside an array from a language file (lang.en.php
):
'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',
helped by a "controller" (localization.php
):
<?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;
?>
The array displays the values in the language files perfectly if I directly open showcase/showcase1.php
, but when it is called from the anchor link using fancybox it says:
Fatal error: Call to undefined function l() in D:\wamp\www\projects\alexchen\alexchen2.6\showcase\showcase1.php on line 3
Any suggestions?
EDIT:
l
is an array in the languages files but i use it as a function later (see at the end of localization.php).
You should be using require_once do these calls. I would hazard that it is not finding localisation.php.
Include will only warn if a file is not present, whereas a require will fatal error. If the program won't work correctly without it, always use require over include.
Is the AJAX version being called from a different path, relatively speaking?
I found out the problem. When localization.php
and langauages/lang.en.php
are being called in showcase/showcase1.php
the PHP code can't file those files. I placed all the showcase files in the root directory and now everything works. Thanks for the responses they helped me figure out the way!
I don't know for sure, but the first thing I'd try would be to copy all the code from localization.php inside showcase1.php. If in that way the AJAX call works, then you could be sure there's a problem with the way fancybox calls the php. Then I'd use the Firebug or live http headers Firefox extensions to check how the showcase1.php is called.
Add an error_reporting(E_ALL); in there. I suspect you're getting another error when trying to include localization.php, and for some reason it isn't showing up. You may also have to enable display_errors in php.ini, or with ini_set().
Use a tool to figure out what is different between calling your script directly, and calling it with AJAX. I recommend Fiddler.
精彩评论