我有一个网址:foo.php?name=adam&lName=scott
,在foo.php
我有一个表单,其中包含rectangleLength
&的值rectangleBreadth
带有提交按钮。
当我点击此提交按钮并将表单操作设为$_SERVER['REQUEST_URI']
时,我会收到此结果网址:foo.php?rectangleLength=10&rectangleBreadth=5
(这些值已由用户填写)。
请注意,我丢失了之前的值name
&来自网址的lName
。
我该如何保留它们?
另外,请记住,我必须回到foo.php
,如果用户想再次提交表单,则长度和广度值应该更改。
答案 0 :(得分:30)
您可以在第一个目标网站上添加两个隐藏字段,在您的情况下为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>
对于动态解决方案,请使用foreach循环:
<?php
foreach($_GET as $name => $value) {
$name = htmlspecialchars($name);
$value = htmlspecialchars($value);
echo '<input type="hidden" name="'. $name .'" value="'. $value .'">';
}
?>
您可以考虑将动态方法锁定为已知可能键的列表:
<?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 .'">';
}
?>
答案 1 :(得分:4)
<form action="<?php echo $_SERVER["PHP_SELF"] . '?'.http_build_query($_GET); ?>" ...
..
..
答案 2 :(得分:3)
有不同的方法可以做到这一点。所有这些都将它们接收的参数写入文件,内存或数据库,然后使用密钥
检索它们最简单的方法就像会话变量:http://php.net/manual/en/features.sessions.php
主要设置是这样的(警告:这是不安全的代码,请确保您只添加要保留的会话变量,并清理用户输入!):
<?php
session_start();
foreach ($_GET as $key=>$value) {
$_SESSION[$key]=>$value;
}
?>
现在,只要用户没有关闭浏览器,您就可以使用$_SESSION[varname];
答案 3 :(得分:2)
有一次,我需要在表格中对结果进行排序,以保持搜索结果来自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>";
答案 4 :(得分:0)
$_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>
但是这个
调用html中的细分 .... 添加转包商 .... 分配转包商合同 ..... 登出 ....
目标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);
}
答案 5 :(得分:0)
以下代码适用于我的项目。希望它有所帮助。 1.在菜单(调用html)中,我调用VendorSearch.php。变量fromvs用于URL。 2.目标php VendorSearch.php将根据$ _GET [&#39; fromvs&#39;]的值执行不同的工作。 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);
}
答案 6 :(得分:0)
我个人的喜好是指定您希望接受的键,并确保通过htmlspecialchars()运行该值。
$url_params = array(
'tab'
);
foreach( $url_params as $key ) {
echo !empty( $_GET[$key] ) ? '<input type="hidden" name="'. $key .'" value="'. htmlspecialchars( $_GET[$key] ) .'" />' : '';
}
答案 7 :(得分:0)
使用数组处理查询:
foreach (explode("\n", http_build_query($query, '', "\n")) as $keyValue) {
[$key, $value] = explode('=', $keyValue, 2);
$key = htmlspecialchars(urldecode($key), ENT_COMPAT | ENT_HTML5);
$value = htmlspecialchars(urldecode($value), ENT_COMPAT | ENT_HTML5);
echo '<input type="hidden" name="' . $key . '" value="' . $value . '"' . "/>\n";
}