所以我正在做的是在File1.php中设置变量$ GLOBALS ['me],在File2.php中,我尝试获取并设置带有该数据的新变量,但是当该变量不起作用时,我通过它,然后它变为空,...但是如果您检查它是否已设置,则表示它已设置。我正在用ajax XMLHttpRequest调用File2.php,我认为这可能是造成这种情况的原因。
--- File1.php ---
$raceid = 2;
$wpm = 1;
$char = 3;
$me = 4;
...
$GLOBALS['me'] = $me;
$content = "char: " . $char . "\n" . "wpm: " . $wpm . "\n" . "me: " . $GLOBALS['me'] . "\n" . "raceId: " . $raceid . "\n";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
//End of php File1.php
如您所见,它将结果打印到文件中,因此我可以看到$ GLOBALS ['me']是什么。所以这是运行后的myText.txt。
--- myText.txt ---
char: 3
wpm: 1
me: 4
raceId: 2
因此,$GLOBALS['me']
等于4(仅在此文件中等于4)非常好,所以这里是PHP File2.php(在执行javascript函数时运行)
--- File2.php ---
//wpm char and race id are defined above in this file.
if (isset($_POST['char']) && isset($_POST['wpm'])){
$char = $_POST['char'];
$wpm = $_POST['wpm'];
if(isset($GLOBALS['raceid'])){
$raceid = $GLOBALS['raceid'];
}
if (isset($GLOBALS['me'])){
$me = $GLOBALS['me'];
$content = "char: ".$char."\n"."wpm: ".$wpm."\n"."me: ".$GLOBALS['me']."\n"."raceId: ".$raceid."\n";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
}
//Other stuff that only executes if $me is a value
}
如您在此文件中看到的,如果设置了$GLOBALS['me']
,那么我将这个文件$ me设置为等于它。然后我再次在文件中设置数据,但是这次文件看起来像这样。
--- myText.txt ---
char: 3
wpm: 1
me:
raceId: 2
现在我是空白(空),我仍在使用$ GLOBALS ??
现在,这里是我的Javascript通过post方法调用File2.php,并通过html按钮单击执行。
--- Javascript ---
function ClickMe(){
$.post("update.php", {
char: 72,
wpm: 123
}, function(data, status){
alert("done!");
});
}
在这些php文件之间,为什么$ GLOBALS ['me']不能保持其价值的任何帮助都将有所帮助,以及如何将$ me变量从php File1转换为php File2。除了发布到File2的脚本外,在这些php文件之间没有其他要求。感谢您的帮助。很抱歉,这个长问题要尽可能地具有描述性,并表明在File1中设置了$ GLOBALS ['me'],在File2中显示为空或为null。谢谢!