我正在尝试在周末禁用表单。在我的代码中,我在变量$noaccess后有一个休息时间。当我运行代码时,表单无法加载。我不确定发生了什么,请帮忙。 这是我的代码:
$wkday = date('D');
if ($wkday == "Sun") {
$noaccess = "yes";
break;
} elseif ($currday == "Sat") {
$noaccess = "yes";
} else {
$noaccess = "no";
}
下面是用于显示窗体的代码:
if ($noaccess == "yes") {
echo "You cannot access this area now";
} else {
//Form displays normally
}
您在elseif ($currday == "Sat")
中引用了错误的变量$currday
在提及$wkday = date('D');
时,应如elseif ($wkday == "Sat")
一样$wkday
另外,正如Dave Goten所说,break;
是不必要的。
如有必要,请添加exit;
、die;
或return;
。
$wkday = date('D');
if ($wkday == "Sun") {
$noaccess = "yes";
} elseif ($wkday == "Sat") {
$noaccess = "yes";
} else {
$noaccess = "no";
}
// test echo
if ($noaccess == "yes") {
echo "You cannot access this area now";
} else {
//Form displays normally
echo $noaccess; // test variable echo
echo "You have access";
}
基本调试(如错误报告)会在给定的日期发出未定义的变量信号。
即:Notice: Undefined variable: currday...
将错误报告添加到文件顶部,这将有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告只应在暂存中完成,而不应在生产环境中完成。
此外,请确保您的表单类似于以下内容:
//Form displays normally
echo "<form action='' method='post'>"; // or get. I have no idea what you're using
echo "<input type='text' name='name'>";
echo "<input type='submit' name='submit'>";
echo "</form>";
。因为您没有将其包含在您的问题中。只是为了确保没有语法错误。
使用
or 运算符来简化逻辑,然后就不需要使用中断。
if ($wkday == "Sun" || $wkday == "Sat") {
$noaccess = "yes";
} else {
$noaccess = "no";
}