如果未设置表单值,但是我无法正常运行,我正在尝试执行一段代码。
这是我的表格:
<form method="post" action="downloadreport.php">
<div class="col-md-12" >
<div class="form-group label-floating">
<label class="control-label">From Date</label>
<input type="text" class="form-control" id="datepicker" name="datepicker" >
</div>
</div>
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">To Date</label>
<input type="text" class="form-control" id="datepicker1" name="datepicker1" >
</div>
</div>
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">Select Usertype</label>
<select name="trackforums" id="trackforumns" class="form-control" >
<option value="1">Active</option>
<option value="0">Inactive</option>
<option value="3">Active/Inactive</option>
</select>
</div>
</div>
<div class="col-md-12">
<div class="form-group label-floating">
<button type="submit" name="submit" value="submit" class="btn btn-info form-control">Download Report</button>
</div>
</div>
</form>
这是我的表单值
if (isset($_POST['submit'])) {
$fromdate =$_POST['datepicker'];
$todate = $_POST['datepicker1'];
$usertype= $_POST['trackforums'];
if($todate >= $fromdate) {
//execute report with dates
} elseif(isset($_POST[fromdate]) && $_POST[todate] = "") {
//execute full report
} else {
echo "Your To date should be greater than from date";
}
}
尝试此操作后,如果我未在表单字段中输入任何日期,则不会进入elseif条件。
答案 0 :(得分:2)
当缺少日期值时,您的表单将传递空字符串; empty()
可以检查。 empty()
提供了检查遗失键的附加好处,因此您不会收到通知。
未测试的建议条件块:
if (!isset($_POST['submit'])) {
echo "No submission";
} elseif (empty($_POST['datepicker']) || empty($_POST['datepicker1'])) {
echo "at least one date picker is empty, treat as no dates picked";
} elseif ($_POST['datepicker1'] >= $_POST['datepicker']) {
echo "invalid To date";
} else {
echo "execute report within valid date range
}