如何在表单提交时保留已设置的 GET 参数值?
我有一个 URL:foo.php?name=adam&lName=scott
,在 foo.php
中,我有一个表单,它给出了 rectangleLength< 的值/代码> &
rectangleBreadth
带有提交按钮。
当我单击表单操作为 $_SERVER['REQUEST_URI']
的提交按钮时,我得到以下结果 URL:foo.php?rectangleLength=10&rectangleBreadth=5
(这些值已由用户填写)。
请注意,我正在丢失以前的值 name
& URL 中的 lName
。
我怎样才能保留它们?
另外,请记住,我必须返回 foo.php,如果用户想要再次提交表单,则长度和宽度值应该更改。
I have a URL : foo.php?name=adam&lName=scott
, and in foo.php
I have a form which gives me values of rectangleLength
& rectangleBreadth
with a submit button.
When I click this submit button with form action as $_SERVER['REQUEST_URI']
, I get this result URL: foo.php?rectangleLength=10&rectangleBreadth=5
(these values have been filled in by the user).
Notice that I am losing my previous values name
& lName
from the URL.
How can I keep them?
Also, keep in mind that I have to come back to foo.php
and if the user wants to submit the form again then the length and breadth values should change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以在第一个目标站点(在您的情况下为 blabla.php)的表单中添加两个隐藏字段:
对于动态解决方案,请使用 foreach 循环:
您可以考虑将动态方法锁定到已知可能键的列表:
You can add two hidden fields in the form on the first target site, blabla.php in your case:
For a dynamic solution, use a foreach loop:
You may consider locking the dynamic approach down to a list of known possible keys:
使用 http_build_query 保持 URL 不变的更简单解决方案
A simpler solution to keep the URL unchanged by using http_build_query
有不同的方法可以做到这一点。它们都将接收到的参数写入文件、内存或数据库,然后使用密钥检索它们。
最简单的方法类似于会话变量:http://php.net/manual/en/features.sessions.php
主要设置是这样的(注意:这是不安全的代码,确保只添加会话变量想要保留并清理用户输入!):
现在,只要用户不关闭浏览器,您就可以使用
$_SESSION[varname];
访问这些变量There are different ways to do this. All of them write the parameters they receive into a file, memory, or a database and retrieve them later with a key
The easiest method is something like a session variable: http://php.net/manual/en/features.sessions.php
The main setup is something like this (caution: that is unsecure code, make sure you only add session variables you want to keep, and sanitize user input!):
and now, as long as the user does not close the browser, you can access these variables with
$_SESSION[varname];
有一次,我需要对表中的结果进行排序,以保留来自 GET 的搜索结果。我确实喜欢这样:
Once, I needed sorting the results in a table keeping the search results coming from GET. I did like that:
使用数组处理查询:
To handle query with arrays:
以下代码适用于我的项目。希望它能帮助一些人。
1.在菜单中(调用html)我调用VendorSearch.php。 URL 中使用变量 fromvs。
2.目标php VendorSearch.php会根据$_GET['fromvs']的值做不同的工作
3. 在VendorSearch.php中,aftersession_start(),
Following code works for my project. Hope it help some.
1. In menu (calling html) I call VendorSearch.php. variable fromvs is used in URL.
2. The target php VendorSearch.php will do different jobs based on the value of $_GET['fromvs']
3. In VendorSearch.php, aftersession_start(),
$_GET['fromvs']
的值做不同的工作在VendorSearch.php中,aftersession_start(),
$srchfor ="";
$fromwhat = $_GET['fromvs'];
$_SESSION['fromwhat'] = $fromwhat;
$vs = $fromwhat;
使用隐藏输入来存储 URL 传递的变量
<输入类型='隐藏'隐藏='隐藏'id='fromvs'name='fromvs'value="" >
But this thie
Segment in Calling html
....
添加分包商
....
分配分包商合同
……
退出
....
目标 php 中的段:VendorSearch.php
$_GET['fromvs']
In VendorSearch.php, aftersession_start(),
$srchfor ="";
$fromwhat = $_GET['fromvs'];
$_SESSION['fromwhat'] = $fromwhat;
$vs = $fromwhat;
Use hidden input to store URL passed variable
<div style='position: absolute; top: 10px; left: 400px;'><input type='hidden' hidden='hidden' id='fromvs' name='fromvs' value="<?php echo $_SESSION['fromwhat']; ?>"></div>
But this thie
Segment in Calling html
....
Add a Subcontractor
....
Assign Subcontractor Contracts
.....
Log Out
....
Segment in target php: VendorSearch.php
我个人的偏好是指定您希望接受的键,并确保通过 htmlspecialchars() 运行该值。
My personal preference would be to specify the keys you wish to accept and be sure to run the value through htmlspecialchars().