如何将PHP全局变量用于2个函数



我正在尝试创建一个界面,在用户创建车间描述后,可以在服务器上更新该界面。有一个文本字段和一个按钮,可按分配的编号显示车间。(这也是服务器上提交/研讨会#中的目录名称(。当我使用此方法设置变量$workshopPath时,我希望在用字符串填充文本输入以更新研讨会标题时能够访问此全局变量。$workshopPath在"updateTextItems"函数中注册为空字符串,因此它将文本文件写入根目录,而不是正确的车间目录。我认为我正确地引用了函数中的全局变量,但这不起作用。我也尝试过使用$GLOBALS["workshopPath"],但也不起作用。有人能帮我弄清楚如何将变量传递给第二个函数吗?谢谢:-(

<?php
$workshopPath;
if (isset($_POST['gotoWorkshop'])) {
if (empty($_POST['numberInput'])) {
echo "Please enter a valid workshop number";
} else { //Assign the name variable form the posted field info if one was entered.
$workshopNumber = stripslashes($_POST['numberInput']);
global $workshopPath;
$workshopPath = "submissions/" . $workshopNumber . "/";
}
}
if (isset($_POST['updateTextItems'])) {
if ($_POST['titleInput']) {
//Assign the name variable form the posted field info if one was entered.
$titleInput = stripslashes($_POST['titleInput']);
}

if ($titleInput) {
global $workshopPath;
$titleFile = fopen($workshopPath . "title.txt", "w") or die("There was an error creating the title file.");
fwrite($titleFile, $titleInput);
fclose($titleFile);
}
}
?>

我理解你的意思对吗?用户填写研讨会的表格点击提交并获得文本的其他表格?我猜你对服务器的请求很敏感。因此$GLOBAL对您不起作用。它只适用于每个请求,我认为大多数时候你并不真正需要它。如果你想在请求之间保存一些值,那么在启动会话时需要一个会话。使用session_start((开始会话后,您可以使用$_session[]存储并获取您的值

最新更新