使用 PHP 将表单字段作为 URL 参数传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18081200/
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
Using PHP to pass a form field as URL parameter
提问by John
I have a form field:
我有一个表单域:
<input type="text" value="" name="Email" id="Email">
<input type="text" value="" name="Email" id="Email">
and my form action confirmation url is this:
我的表单操作确认网址是这样的:
http://.../index.php?Email=<?php echo $_POST['Email']; ?>
http://.../index.php?Email=<?php echo $_POST['Email']; ?>
However after submitting, the Email parameter is not coming through. Is this something that can be done in PHP, or does it only read the field on initial page load?
但是提交后,电子邮件参数没有通过。这是可以在 PHP 中完成的事情,还是仅在初始页面加载时读取该字段?
Thanks
谢谢
回答by jterry
You don't need to define the structure of the GET
request; that's what the form does.
您不需要定义GET
请求的结构;这就是表格的作用。
For instance:
例如:
<form action="workerbee.php" method="GET">
<input type="text" name="honey_type" value="sweet" />
</form>
...when submitted, would automatically append the field - honey_type
- to the URL for you. It would end up like this:
...提交后,会自动将字段 - honey_type
-附加到您的 URL。最终会是这样:
http://example.com/workerbee.php?honey_type=sweet
You can then access the value via $_GET['honey_type']
in workerbee.php
. To pre-fill the form with the existing submitted value - assuming that workerbee.php
holds the form - just add a conditional value
parameter:
然后您可以通过$_GET['honey_type']
in访问该值workerbee.php
。要使用现有的提交值预填充表单 - 假设workerbee.php
保存表单 - 只需添加一个条件value
参数:
<?php
$honey_type = !empty($_GET['honey_type']) ? $_GET['honey_type'] : null;
?>
<input type="text" name="honey_type" value="'<?php echo htmlspecialchars($honey_type); ?>'" />
回答by Sandesh Daddi
It depends on the your FORM method
这取决于您的 FORM 方法
Your form should be
你的表格应该是
<form method='post' action='http://.../index.php'>
<input type="text" value="" name="Email" id="Email">
<input type='submit' value='Post data'>
</form>
and to access email in index.php you can write code as below
并在 index.php 中访问电子邮件,您可以编写如下代码
<?php
$emailValue = $_POST["Email"];
//Use variable for further processing
?>
if your form is as below (please check that method is get
如果您的表格如下(请检查该方法是获取
<form method='get' action='http://.../index.php'>
<input type="text" value="" name="Email" id="Email">
<input type='submit' value='Post data'>
</form>
and to access email in index.php you can write code as below
并在 index.php 中访问电子邮件,您可以编写如下代码
<?php
$emailValue = $_GET["Email"];
//Use variable for further processing
?>
回答by Aron Cederholm
Your issue is that you are mixing $_GET and $_POST.
您的问题是您正在混合 $_GET 和 $_POST。
See your code here, http://.../index.php?Email=<?php echo $_POST['Email']; ?>
, when you post to that one, there will no longer be a $_POST['Email'], but a $_GET['Email']. So the first post will likely work (if you are using <form method="post" action="...">
), but the second submit will fail, as $_POST['Email']
no longer exists.
在这里查看您的代码http://.../index.php?Email=<?php echo $_POST['Email']; ?>
,当您发布到该代码时,将不再有 $_POST['Email'],而是 $_GET['Email']。因此,第一篇文章可能会起作用(如果您正在使用<form method="post" action="...">
),但第二篇文章将失败,因为$_POST['Email']
不再存在。
So I recommend that you don't use parameters in the action. Instead, put them in a hidden field or switch to only $_GET
parameters.
所以我建议你不要在动作中使用参数。相反,将它们放在隐藏字段中或仅切换到$_GET
参数。
Option 1, use hidden field
选项 1,使用隐藏字段
Change the form on your second page to:
将第二页上的表格更改为:
<form action="http://.../index.php" method="POST">
<input type="hidden" name="Email" id="Email" value="<?php echo $_POST['Email'];?>" />
...
</form>
Option 2, use only $_GET
选项 2,仅使用 $_GET
Change the form on your first page to <form ... method="GET">
将第一页上的表格更改为 <form ... method="GET">
And then change the form on the second page to use $_GET['Email']
and the method to GET
.
然后将要使用的第二页上的表单$_GET['Email']
和方法更改为GET
.
<form action="http://.../index.php??Email=<?php echo $_GET['Email'];?>" method="GET">
...
</form>
Option 3, Use $_REQUEST instead of $_POST
选项 3,使用 $_REQUEST 而不是 $_POST
Simply use http://.../index.php?Email=<?php echo $_REQUEST['Email']; ?>
as your action url, as $_REQUEST is a merge of $_GET and $_POST. Be aware that this is a merge of $_GET, $_POST and $_COOKIE.
只需http://.../index.php?Email=<?php echo $_REQUEST['Email']; ?>
用作您的操作网址,因为 $_REQUEST 是 $_GET 和 $_POST 的合并。请注意,这是 $_GET、$_POST 和 $_COOKIE 的合并。
回答by IanPudney
If you're trying to use data from your current form, your form tag should read as follows:
如果您尝试使用当前表单中的数据,您的表单标签应如下所示:
<form action="http://.../index.php" method="GET">
If you're trying to pass data your server already has (such as from a previous form), then you should use a hidden field instead:
如果您尝试传递服务器已有的数据(例如来自以前的表单),则应改用隐藏字段:
<input name="email" type="hidden" value="<?php echo $_POST['Email']; ?>">