提交表单将替换页面所需的常量GET信息 [英] Submitting form replaces constant GET information that is needed for the page
问题描述
我正在学习php,我必须说我到处都是google,但是我被困住了:(
I'm learning php and I've got to say I've googled everywhere and I'm stuck :(
换句话说,我的问题是如何将新变量添加到页面上的先前变量?
My question is, in other words, how can I get new variables added to previous variables on the page?
想象一下这是 www.mysite.com/getvartest.php?orgvar=12345
但提交此表格后,它将转到
but after submitting this form it'll go to
www.mysite.com/getvartest.php?varselection=selection1
我期望的是:
想象一下这是 www.mysite.com/getvartest.php?orgvar=12345&varselection=selection1
我该如何解决?
<p>Testing two ore more variables</p>
<form action="getvartest.php?orgvar=12345" method="get">
<select name="varselection">
<option value="selection1">selection1</option>
<option value="selection2">selection2</option>
</select>
<input type="submit" />
</form>
<?php
@$originalvar = $_GET['orgvar'];
@$varselection = $_GET['varselection'];
if($originalvar&&$varselection)
echo "Testing original variable $originalvar. Testing second passthrough variable through here: $varselection";
?>
推荐答案
使用部分隐藏的输入字段来保存值,而不是提交给带有部分查询字符串的 action
URL您要修复的变量.看看是否可以让您的PHP脚本生成这样的表单:
Instead of submitting to an action
URL with part of the query string already filled out, use a hidden input field to hold the values of the variables you want to be fixed. See if you can get your PHP script to generate a form like this:
<form action="getvartest.php" method="get">
<input type="hidden" name="orgvar" value="12345" />
<select name="varselection">
<option value="selection1">selection1</option>
<option value="selection2">selection2</option>
</select>
<input type="submit" />
</form>
也许是这样的:
<input type="hidden" name="orgvar" value="<?= htmlspecialchars($_GET['orgvar']) ?>" />
( htmlspecialchars()
调用仅在其中转义<
,&
和"
情况在 orgvar
中有这些.)
(The htmlspecialchars()
call escapes characters like <
, &
, and "
just in case there are any of these in orgvar
.)
或者,如果您想保存每个查询参数,则可以使用通用循环,例如:
Or if you wanted to save every query parameter you could use a generic loop like:
<?php foreach ($_GET as $name => $value) { ?>
<input type="hidden" name="<?= htmlspecialchars($name) ?>"
value="<?= htmlspecialchars($value) ?>" />
<?php } ?>
这篇关于提交表单将替换页面所需的常量GET信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!