php 如何在表单提交时保留已设置的 GET 参数值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7201124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to keep already-set GET parameter values on form submission?
提问by saurabh
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.
我有一个 URL : foo.php?name=adam&lName=scott
,在foo.php
我有一个表单,它给我rectangleLength
& 的值和rectangleBreadth
一个提交按钮。
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).
当我单击此提交按钮的表单操作为 时$_SERVER['REQUEST_URI']
,我得到此结果 URL:(foo.php?rectangleLength=10&rectangleBreadth=5
这些值已由用户填写)。
Notice that I am losing my previous values name
& lName
from the URL.
请注意,我失去了我的前值name
和lName
从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.
另外,请记住,我必须回来foo.php
,如果用户想再次提交表单,那么长度和宽度值应该改变。
回答by Emil Vikstr?m
You can add two hidden fields in the form on the first target site, blabla.php in your case:
您可以在第一个目标站点 blabla.php 的表单中添加两个隐藏字段:
<form ...>
<input type="hidden" name="name" value="<?php echo htmlspecialchars($_GET['name']);?>">
<input type="hidden" name="lName" value="<?php echo htmlspecialchars($_GET['lName']);?>">
<!-- rest of the form here -->
</form>
For a dynamic solution, use a foreach loop:
对于动态解决方案,请使用 foreach 循环:
<?php
foreach($_GET as $name => $value) {
$name = htmlspecialchars($name);
$value = htmlspecialchars($value);
echo '<input type="hidden" name="'. $name .'" value="'. $value .'">';
}
?>
You may consider locking the dynamic approach down to a list of known possible keys:
您可以考虑将动态方法锁定到已知可能的键列表:
<?php
$keys = array('name', 'lName', ...);
foreach($keys as $name) {
if(!isset($_GET[$name])) {
continue;
}
$value = htmlspecialchars($_GET[$name]);
$name = htmlspecialchars($name);
echo '<input type="hidden" name="'. $name .'" value="'. $value .'">';
}
?>
回答by Kalyanaraman Santhanam
A simpler solution to keep the URL unchanged by using http_build_query
使用http_build_query保持 URL 不变的更简单的解决方案
<form action="<?php echo $_SERVER["PHP_SELF"] . '?'.http_build_query($_GET); ?>" ...
..
..
回答by ty812
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
最简单的方法类似于会话变量: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!):
主要设置是这样的(注意:这是不安全的代码,确保您只添加要保留的会话变量,并清理用户输入!):
<?php
session_start();
foreach ($_GET as $key=>$value) {
$_SESSION[$key]=>$value;
}
?>
and now, as long as the user does not close the browser, you can access these variables with $_SESSION[varname];
现在,只要用户不关闭浏览器,就可以访问这些变量 $_SESSION[varname];
回答by Sarvar Nishonboev
Once, I needed sorting the results in a table keeping the search results coming from GET. I did like that:
有一次,我需要对表格中的结果进行排序,以保持来自 GET 的搜索结果。我是这样的:
unset($_GET['sort']); // sort param is removed, otherwise there will be created many sort params
$url = http_build_query($_GET);
echo "<a href='?".$url."&sort=title'>Title</a>";
echo "<a href='?".$url."&sort=author'>Author</a>";
回答by Joel Lisenby
My personal preference would be to specify the keys you wish to accept and be sure to run the value through htmlspecialchars().
我个人的偏好是指定您希望接受的键,并确保通过 htmlspecialchars() 运行该值。
$url_params = array(
'tab'
);
foreach( $url_params as $key ) {
echo !empty( $_GET[$key] ) ? '<input type="hidden" name="'. $key .'" value="'. htmlspecialchars( $_GET[$key] ) .'" />' : '';
}
回答by Peter Moh
- In menu (calling html) I call VendorSearch.php. variable fromvs is used in URL.
- The target php VendorSearch.php will do different jobs based on the value of
$_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
- 在菜单(调用 html)中,我调用 VendorSearch.php。URL 中使用了变量 fromvs。
- 目标 php VendorSearch.php 将根据
$_GET['fromvs']
在 VendorSearch.php 中,aftersession_start(),
$srchfor =""; $fromwhat = $_GET['fromvs']; $_SESSION['fromwhat'] = $fromwhat; $vs = $fromwhat;
使用隐藏输入来存储 URL 传递的变量
<div style='position: absolute; top: 10px; left: 400px;'><input type='hidden' hidden='hidden' id='fromvs' name='fromvs' value="<?php echo $_SESSION['fromwhat']; ?>"></div>
但是这个贼
Segment in Calling html .... Add a Subcontractor .... Assign Subcontractor Contracts ..... Log Out ....
Segment in Calling html .... 添加分包商 .... 分配分包商合同 ..... 注销 ....
Segment in target php: VendorSearch.php
目标 php 中的段:VendorSearch.php
<?php
//VendorSearch.php
//http://mted202.mtaent.org:9051/ocr/login.php rweinbau
require_once('dbinfo.php');
session_start();
$c = oci_pconnect("ocr","ocrmta","HQT4");
oci_set_client_identifier($c, $_SESSION['username']);
$username = htmlentities($_SESSION['username'], ENT_QUOTES);
.....
$srchfor ="";
$fromwhat = $_GET['fromvs'];
$_SESSION['fromwhat'] = $fromwhat;
$vs = $fromwhat;
if (isset($_POST['srchvnd']))
{
$vs = $_POST['fromvs'];
somefunction($vs);
}
else
{
;
}
?>
<body>
<form class="vfrmsrch" name="vndsearch" id="vndsearch" action="VendorSearch.php?fromvs='<?php echo $fromwhat; ?>'" method="POST">
<div style='position: absolute; top: 10px; left: 400px;'><input type='hidden' hidden='hidden' id='fromvs' name='fromvs' value="<?php echo $_SESSION['fromwhat']; ?>"></div>
......
</form>
.......
</body>
</html>
<?php
function somefunction($vvs){
//$msg = "We are inf somefunction() function </a></div><br>";
// echo "<div style='position: absolute; top: 100px; left: 10px;'><a style='color:blue'>".$msg;
$_SESSION['fromwhat'] = $vvs;
............
oci_close($c);
}
回答by Peter Moh
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(),
以下代码适用于我的项目。希望能帮到一些人。1. 在菜单中(调用 html)我调用 VendorSearch.php。URL 中使用了变量 fromvs。2. 目标 php VendorSearch.php 会根据 $_GET['fromvs'] 的值做不同的工作 3. 在 VendorSearch.php 中,aftersession_start(),
$srchfor ="";
$fromwhat = $_GET['fromvs'];
$_SESSION['fromwhat'] = $fromwhat;
//save value to $VS
$vs = $fromwhat;
3. 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>
4. But this thie field's value may lost after clicking button "srchvnd". So use a function to reset
$_SESSION['fromwhat'];
if (isset($_POST['srchvnd']))
{
$vs = $_POST['fromvs'];
somefunction($vs);
}
-----------------Source code----------------------
Segment in Calling html
....
<body>
<div style=" position: absolute; top: 1px; left: 5px; height:740px; width:205px; border-radius: 10px;" >
<!-- Start css3menu.com BODY section -->
<ul id="css3menu1" class="topmenu">
<li class="topfirst"><a href="VendorSearch.php?fromvs=V" target="I1" style="width:183px;">Add a Subcontractor </a></li>
....
<li class="topmenu"><a href="VendorSearch.php?fromvs=S" target="I1" style="width:183px;">Assign Subcontractor Contracts</a></li>
.....
<li class="toplast"><a href="login.php" target="_self" style="width:183px;">Log Out</a></li>
</ul>
....
</div>
Segment in target php: VendorSearch.php
<?php
//VendorSearch.php
//http://mted202.mtaent.org:9051/ocr/login.php rweinbau
require_once('dbinfo.php');
session_start();
$c = oci_pconnect("ocr","ocrmta","HQT4");
oci_set_client_identifier($c, $_SESSION['username']);
$username = htmlentities($_SESSION['username'], ENT_QUOTES);
.....
$srchfor ="";
$fromwhat = $_GET['fromvs'];
$_SESSION['fromwhat'] = $fromwhat;
$vs = $fromwhat;
if (isset($_POST['srchvnd']))
{
$vs = $_POST['fromvs'];
somefunction($vs);
}
else
{
;
}
?>
<body>
<form class="vfrmsrch" name="vndsearch" id="vndsearch" action="VendorSearch.php?fromvs='<?php echo $fromwhat; ?>'" method="POST">
<div style='position: absolute; top: 10px; left: 400px;'><input type='hidden' hidden='hidden' id='fromvs' name='fromvs' value="<?php echo $_SESSION['fromwhat']; ?>"></div>
......
<td><input type="submit" class="slbt" name="srchvnd" id ="srchvnd" vaue="Search"></input></td>
......
</form>
.......
</body>
</html>
<?php
function somefunction($vvs){
//$msg = "We are inf somefunction() function </a></div><br>";
// echo "<div style='position: absolute; top: 100px; left: 10px;'><a style='color:blue'>".$msg;
$_SESSION['fromwhat'] = $vvs;
............
oci_close($c);
}