HTML表单从PHP页面获取两个变量
我已经工作了好几个小时试图让它正常工作。我有一个表单的页面是HTML表单从PHP页面获取两个变量,php,html,forms,get,Php,Html,Forms,Get,我已经工作了好几个小时试图让它正常工作。我有一个表单的页面是/index.php?action=pagename。我有一个表单,需要从下面的/index.php?action=pagename&thing=something中获取一个变量。我的HTML表单如下所示: <form role="form" action="pagename" method="get"> <input type="text" name="thing"> &l
/index.php?action=pagename
。我有一个表单,需要从下面的/index.php?action=pagename&thing=something
中获取一个变量。我的HTML表单如下所示:
<form role="form" action="pagename" method="get">
<input type="text" name="thing">
</form>
此表单位于/index.php?action=pagename
上,我想从该URL获取&thing
。
有什么想法吗
我遇到的问题是,在提交表单时,URL重定向到
index.php?thing
,而不是停留在index.php?action=pagename
,您只需使用php变量$\u GET['thing']
获取值。看起来您可能在使用
时遇到了一些问题,而不仅仅是PHP,因此这里是一个概述:
<!-- the action is where you want to send the form data -->
<!-- assuming THIS code is the index.php file then we want to send the data to ourselves -->
<!-- the method is GET so it will be directly accessible from the URL later -->
<form action="index.php" method="GET">
<!-- add a hidden value for pagename -->
<input type="hidden" name="action" value="pagename">
<!-- the name called "thing" will be appended to the URL and it's value as well -->
<input type="text" name="thing" value="<?php echo (isset($_GET['thing']) ? $_GET['thing'] : ''); ?>">
<br>
<!-- click this button to submit the form to itself -->
<!-- once this has been submitted then you can retrieve the URL value with $_GET as you can see above -->
<input type="submit" value="submit" />
</form>
表单元素的action属性是在提交时调用的目标脚本。如果为空,则显示表单的当前脚本。您也只能提供以?开头的url参数
<form role="form" action="?action=pagename" method="get">
<input type="text" name="thing">
</form>
我为从URL获取变量而编写的基本脚本:
<?php
if (isset($_GET["action"]) && isset($_GET["thing"])) {
$action = $_GET["action"];
$thing = $_GET["thing"];
echo $action .PHP_EOL . $thing;
}
?>
尽管您可能应该首先学习如何正确使用表单。“此表单位于/index.php?action=pagename上,我想从该URL获取&thing。”-thing
在/index.php?action=pagename
中不存在,将传递给$thing=thisvalue
的值可用作表单中输入的值。当您将&thing
添加到index.php?action=pagename
时,将根据&thing
加载新内容。这里的目的是使用表单搜索&thing
,以便用户可以通过手动添加&thing
加载内容。我已经用我遇到的确切问题更新了问题。我不知道为什么我会被如此严厉的否决。我使用了这个,结果是URL重定向回index.php?thing='result'
很抱歉,我通常不使用method=“GET”
,但我认为更新后的代码现在应该可以工作了谢谢你跳出框框思考,你太棒了!不客气,不要让反对票让你气馁,因为我还清楚地记得我与
斗争的时候,所以希望你坚持下去,因为web开发总体上有一个光明的未来。
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
Hi Joe. You are 22 years old.
<?php
if (isset($_GET["action"]) && isset($_GET["thing"])) {
$action = $_GET["action"];
$thing = $_GET["thing"];
echo $action .PHP_EOL . $thing;
}
?>
pagename
something