【发布时间】:2020-08-09 15:22:19
【问题描述】:
我正在尝试创建一个界面,用户创建后可以在服务器上更新研讨会描述。有一个文本字段和一个按钮,可以按分配的编号调出研讨会。 (这也是服务器上submissions/workshop#中的目录名称)。当我使用此方法设置变量 $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);
}
}
?>
【问题讨论】:
-
你没有
updateTextItems函数?
标签: php function scope global-variables fwrite