我对下面的代码有问题。为什么当我测试$date变量时,if语句的返回值总是为真?这段代码的目的是在第一次加载页面时检查$date变量是否为空。如果页面是第一次加载,$date变量将返回当前日期,如果我从form中选择了一个日期,它将加载相同的页面,变量$date将是选择的日期。
<form action="#" method="POST">
<p>Select date: <input type="text" id="datepicker" name="datepicker" style="display: inline !important; width: 100px;">
<input type="submit" value="Ok"></p>
<?php
//Variable to return the value of datepicker (calendar)
if (empty($date)) { //condition to check if the $date variable is empty, if empty select current date and send value to another php script
$date=date("Y-m-d")."%"; //select current date
$_SESSION['sending_date'] = $date;
} else{// if the value of date was selected, send variable value to another php script
$_SESSION['sending_date'] = $_POST['datepicker'] . "%";
}
?>
<div id="draw_chart"></div>
</form>
如果$_POST['datepicker']
为空,我认为您正在尝试设置$_SESSION['sending_date']
。
基于这个假设,修改后的if语句应该是:if (empty($_POST['datepicker']))