来自日期选择器的输入会破坏下拉列表中的输入
Posted
技术标签:
【中文标题】来自日期选择器的输入会破坏下拉列表中的输入【英文标题】:Input from datepicker breaks inputs from dropdown lists 【发布时间】:2017-08-23 09:26:35 【问题描述】:我为表格创建了一个过滤器,起初它工作得很好,但是当我用日期选择器替换日期的输入表单时,我注意到只有涉及日期的过滤器有效,而另一个被忽略并且我看不出它为什么会这样。
这是为过滤器选项创建下拉列表的部分:
echo "<form action='listTimes.php' method='post'>
<table>
<caption><b>Filter By:</b></caption>
<tr>
<th>Task Name</th>
<th>Task Stage</th>
<th>Name</th>
<th>Date</th>
</tr>
<tr>
<td>";
// first filter option - dropdown
$sql = "SELECT DISTINCT taskName FROM tasks";
$result = mysqli_query($link, $sql);
echo "<select name='taskName'>";
echo "<option selected='selected' value='' disabled='disabled'></option>";
while ($row = mysqli_fetch_array($result))
if (isset($row['taskName']))
echo "<option value='" . $row['taskName'] . "'>" . $row['taskName'] . "</option>";
echo "</select>
</td>
<td>";
// second filter option - dropdown
$sql = "SELECT DISTINCT taskStage FROM taskattributes";
$result = mysqli_query($link, $sql);
echo "<select name='taskStage'>
<option selected='selected' value='' disabled='disabled'></option>";
while ($row = mysqli_fetch_array($result))
if (isset($row['taskStage']))
echo "<option value='" . $row['taskStage'] . "'>" . $row['taskStage'] . "</option>";
echo "</select>
</td>
<td>";
// third filter option - dropdown
$sql = "SELECT DISTINCT userName FROM users";
$result = mysqli_query($link, $sql);
echo "<select name='userName'>";
echo "<option selected='selected' value='' disabled='disabled'></option>";
while ($row = mysqli_fetch_array($result))
if (isset($row['userName']))
echo "<option value='" . $row['userName'] . "'>" . $row['userName'] . "</option>";
/* fourth filter option, was just a simple text input at first. Now I changed it to a datepicker, which works, but the other three filter options stopped working at this point */
echo "</select>
</td>
<td>
<input type='text' class='datepicker' name='entryDate'>
</td>
</tr>
</table>
<div align='center'> <input type='submit' value='Filter'></div>
</form>
后面的过滤机制写的很烂,但是效果很好:
if(!isset($_POST['taskName']) && !isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
$sql = "SELECT * FROM timeEntry";
$result = $link->query($sql);
elseif (isset($_POST['taskName']) && !isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName'";
$result = $link->query($sql);
elseif (isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND taskStage='$taskStage'";
$result = $link->query($sql);
elseif (isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND taskStage='$taskStage' AND userName='$userName'";
$result = $link->query($sql);
elseif (isset($_POST['taskName']) && isset($_POST['taskStage']) && isset($_POST['userName']) && isset($_POST['entryDate']))
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND taskStage='$taskStage' AND userName='$userName' AND entryDate='$entryDate'";
$result = $link->query($sql);
elseif (!isset($_POST['taskName']) && isset($_POST['taskStage']) && isset($_POST['userName']) && isset($_POST['entryDate']))
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE taskStage='$taskStage' AND userName='$userName' AND entryDate='$entryDate'";
$result = $link->query($sql);
elseif (!isset($_POST['taskName']) && !isset($_POST['taskStage']) && isset($_POST['userName']) && isset($_POST['entryDate']))
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE userName='$userName' AND entryDate='$entryDate'";
$result = $link->query($sql);
elseif (!isset($_POST['taskName']) && !isset($_POST['taskStage']) && !isset($_POST['userName']) && isset($_POST['entryDate']))
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE entryDate='$entryDate'";
$result = $link->query($sql);
elseif (!isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && isset($_POST['entryDate']))
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE taskStage='$taskStage'AND entryDate='$entryDate'";
$result = $link->query($sql);
elseif (isset($_POST['taskName']) && !isset($_POST['taskStage']) && isset($_POST['userName']) && !isset($_POST['entryDate']))
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND userName='$userName'";
$result = $link->query($sql);
elseif (isset($_POST['taskName']) && !isset($_POST['taskStage']) && isset($_POST['userName']) && isset($_POST['entryDate']))
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND userName='$userName' AND entryDate='$entryDate'";
$result = $link->query($sql);
elseif (!isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$sql = "SELECT * FROM timeEntry WHERE taskStage='$taskStage'";
$result = $link->query($sql);
elseif (isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && isset($_POST['entryDate']))
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND taskStage='$taskStage' AND entryDate='$entryDate'";
$result = $link->query($sql);
elseif (!isset($_POST['taskName']) && !isset($_POST['taskStage']) && isset($_POST['userName']) && !isset($_POST['entryDate']))
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$sql = "SELECT * FROM timeEntry WHERE userName='$userName'";
$result = $link->query($sql);
elseif (isset($_POST['taskName']) && !isset($_POST['taskStage']) && !isset($_POST['userName']) && isset($_POST['entryDate']))
$taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));
$entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
$sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND entryDate='$entryDate'";
$result = $link->query($sql);
elseif (!isset($_POST['taskName']) && isset($_POST['taskStage']) && isset($_POST['userName']) && !isset($_POST['entryDate']))
$taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
$userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
$sql = "SELECT * FROM timeEntry WHERE taskStage='$taskStage' AND userName='$userName'";
$result = $link->query($sql);
echo "<table class='top1' border='1'>
<tr>
<th>Entry No.</th>
<th>Task/Activity</th>
<th>Task Stage</th>
<th>User Name</th>
<th>Time Spent</th>
<th>Date</th>
<th>Edit Entry</th>
<th>Delete Entry</th>
</tr>";
if ($result->num_rows > 0)
// output data of each row
while($row = $result->fetch_assoc())
echo "<tr>
<td align='center'>" . $row['timeId'] . "
</td>";
echo "<td>" . $row['taskName'] . "</td>";
echo "<td>" . $row['taskStage'] . "</td>";
echo "<td>" . $row['userName'] . "</td>";
echo "<td align='center'>" . $row['timeSpent'] . "</td>";
$originaldate = mysqli_real_escape_string($link, $row['entryDate']);
$newdate = date("d/m/Y", strtotime($row['entryDate']));
echo "<td align='center'>" . $newdate . "</td>";
echo "<td align='center'><a href='editAllTimes.php?timeId=" . $row["timeId"] . "'>Edit</a></td>";
echo "<td align='center'><a href='deleteAllTime.php?timeId=" . $row["timeId"] . "' onclick='return checkDelete()'>Delete</a></td>
</tr>";
echo "</table>";
这段代码看起来很糟糕,在添加日期选择器之前运行良好:
<input type='text' class='datepicker' name='entryDate'>
我注意到一个奇怪的行为:
-
如果我删除名称属性 name='entryDate' 其他过滤器和日期选择器也可以工作,但是当我按下过滤器时没有记录日期,因此日期过滤器不起作用.
如果我保留名称属性name='entryDate',其他三个过滤器将停止工作,只有日期过滤器有效。
这种行为有什么原因吗?有什么解决办法吗?
【问题讨论】:
再想一想,起初日期过滤器也是一个下拉列表<select>
,而不是<input>
表单,这就是它起作用的原因。用 <input>
替换它是导致问题的原因,因为我正在使用 isset 测试表单的值。
【参考方案1】:
我认为<input>
元素的值始终显示在 $_POST 中,无论是否填充。您应该尝试使用isset() 和empty() 检查该字段。
【讨论】:
谢谢老兄,成功了! 总是设置的,所以用 isset 测试它是错误的。相反,我们应该使用 empty()。我所要做的就是替换:isset($_POST['entryDate'])
与 !empty($_POST['entryDate']))
和 !isset($_POST['entryDate'])
与 empty($_POST['entryDate']))
以上是关于来自日期选择器的输入会破坏下拉列表中的输入的主要内容,如果未能解决你的问题,请参考以下文章
如何显示日期来自jQuery instated显示当前日期?