[英]Keep input field on the next page
I have a date filter for entries from database.我有一个用于数据库条目的日期过滤器。 On the first page i have a list of all entries.
在第一页上,我有一个所有条目的列表。 On the second i have the same filter and almost filtered by date list.
在第二个我有相同的过滤器,几乎按日期列表过滤。 So i want to keep values of filter from the first page.
所以我想从第一页保留过滤器的值。
Here is my code :这是我的代码:
<form action="filter.php" method="post"> <label>From</label><input name="from" required="true" type="date"/> <label>To:</label> <input name="to" required="true" type="date"/> <input name="" type="submit" value="Search" /> </form> <table> ... ... ... </table>
You can access the variable after pressing submit.. Therfore you have to change <input name="" type="submit" value="Search" />
to <input name="submit" type="submit" value="Search" />
您可以在按下提交后访问该变量。因此,您必须将
<input name="" type="submit" value="Search" />
更改为<input name="submit" type="submit" value="Search" />
<?php
if(isset($_POST['submit'])) {
$from = $_POST['from'];
$to = $_POST['to'];
}
?>
You can also store the variable into sessions, to access it from everywhere.您还可以将变量存储到会话中,以便从任何地方访问它。
<?php
session_start();
if(isset($_POST['submit'])) {
$from = $_POST['from'];
$to = $_POST['to'];
$_SESSION['from'] = $from;
$_SESSION['to'] = $to;
}
?>
The third option would be, to set the method of the form element to method="GET"
.第三个选项是,将表单元素的方法设置为
method="GET"
。 After that you can access the values through:之后,您可以通过以下方式访问这些值:
<?php
if(isset($_GET['submit'])) {
$from = $_GET['from'];
$to = $_GET['to'];
}
?>
With the get Parameter you can also do a magic thing like: http://www.example.com/index.php?from=20150906&to=20150907 .使用 get 参数,您还可以做一些神奇的事情,例如: http://www.example.com/index.php?from=20150906&to=20150907 。 So you get the values of the variable via the URL..
所以你通过 URL 获取变量的值..
For more information see: http://php.net/manual/en/reserved.variables.php有关更多信息,请参阅: http : //php.net/manual/en/reserved.variables.php
I hope I could help you...我希望我能帮到你...
In your case, since you are using POST
, you can access the values在您的情况下,由于您使用的是
POST
,您可以访问这些值
if(isset($_POST['submit']])) {
$from = $_POST['from'];
$to = $_POST['to'];
}
As user Markus already said, you should change your submit-button
name-attribute to a self-explanatory name, to keep it easy to maintain the code and check, if this button was pressed.正如用户 Markus 已经说过的,您应该将
submit-button
名称属性更改为一个不言自明的名称,以便易于维护代码并检查是否按下了该按钮。
You then have to set it via the value
-attribute in your form.然后,您必须通过表单中的
value
-attribute 设置它。
<form action="filter.php" method="post">
<label>From</label><input name="from" value="<?php echo $from; ?> required="true" type="date"/>
<label>To:</label> <input name="to" value="<?php echo $from; ?>required="true" type="date"/>
<input name="submit" type="submit" value="Search" />
</form>
Keep in mind, that it is never safe to pass unsanitized data.请记住,传递未经消毒的数据永远是不安全的。 So you should consider sanitizing the user input.
所以你应该考虑清理用户输入。 (for instance a given format like YYYY-MM-DD)
(例如给定的格式,如 YYYY-MM-DD)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.