使用PHP将表单字段作为URL参数传递

使用PHP将表单字段作为URL参数传递,php,forms,post,Php,Forms,Post,我有一个表单字段: 我的表单操作确认url如下: http://.../index.php?Email= 但是,提交后,电子邮件参数无法通过。这是可以在PHP中完成的,还是只在初始页面加载时读取字段 谢谢如果您试图使用当前表单中的数据,则表单标签应如下所示: <form action="http://.../index.php" method="GET"> 如果您试图传递服务器已有的数据(例如来自上一个表单),则应使用隐藏字段: <input name="email"

我有一个表单字段:

我的表单操作确认url如下:

http://.../index.php?Email=

但是,提交后,电子邮件参数无法通过。这是可以在PHP中完成的,还是只在初始页面加载时读取字段


谢谢

如果您试图使用当前表单中的数据,则表单标签应如下所示:

<form action="http://.../index.php" method="GET">

如果您试图传递服务器已有的数据(例如来自上一个表单),则应使用隐藏字段:

<input name="email" type="hidden" value="<?php echo $_POST['Email']; ?>">

您不需要定义
GET
请求的结构;这就是表单的作用

例如:

<form action="workerbee.php" method="GET">
    <input type="text" name="honey_type" value="sweet" />
</form>
然后,您可以通过
workerbee.php
中的
$\u GET['honey\u type']
访问该值。要使用现有提交的值预填充表单-假设
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); ?>'" />


这取决于您的表单方法

你的表格应该是

<form method='post' action='http://.../index.php'>
<input type="text" value="" name="Email" id="Email">
<input type='submit' value='Post data'>
</form>

要访问index.php中的电子邮件,您可以编写如下代码

<?php
 $emailValue = $_POST["Email"];
//Use variable for further processing

?>
<?php
 $emailValue = $_GET["Email"];
//Use variable for further processing

?>

如果您的表格如下(请检查方法是否为get

<form method='get' action='http://.../index.php'>
<input type="text" value="" name="Email" id="Email">
<input type='submit' value='Post data'>
</form>

要访问index.php中的电子邮件,您可以编写如下代码

<?php
 $emailValue = $_POST["Email"];
//Use variable for further processing

?>
<?php
 $emailValue = $_GET["Email"];
//Use variable for further processing

?>

你的问题是你把$\u GET和$\u POST混在一起了

请在此处查看您的代码,
http://.../index.php?Email=
,当您发布到该帖子时,将不再有$\u post['Email'],而是$\u GET['Email']。因此第一篇帖子可能会工作(如果您使用的是
),但第二次提交将失败,因为
$\u post['Email']
不再存在

因此,我建议您不要在操作中使用参数,而是将它们放在隐藏字段中或切换到仅
$\u GET
参数

选项1,使用隐藏字段 将第二页上的表格更改为:

<form action="http://.../index.php" method="POST">
    <input type="hidden" name="Email" id="Email" value="<?php echo $_POST['Email'];?>" />
    ...
</form>
选项3,使用$\u请求而不是$\u POST
只需使用
http://.../index.php?Email=
作为您的操作url,as$\u请求是$\u GET和$\u POST的合并。请注意,这是$\u GET、$\u POST和$\u COOKIE的合并。

表单中使用了哪种HTTP方法?我想它是POST,而您的电子邮件值将通过GET传递。是否使用action=“GET”或action=“POST”?使用表单方法“GET”而不是POST,并且在操作中参数只需键入http://.../index.php"第一件事:你为什么想要它?谢谢你的回复。不幸的是,我仅限于使用POST操作,因为它是通过第三方程序传递的,稍后确认链接将作为表单字段传递。只有在URL中已经存在参数的情况下,才能进入URL,所以除非我使用JS或其他东西,否则看起来我就是SOL来填充确认链接。您的选项2对我无效…您确定它当前也有效吗