简体   繁体   English

正确使用$ _POST的方式

[英]Correct Way to Use $_POST

If I have three PHP pages, lets call them one.php, two.php, and three.php, and I use something like this on the first page: 如果我有三个PHP页面,则将它们称为one.php,two.php和three.php,并且在第一页上使用类似以下的内容:

<form name = "one" action = "two.php" onsubmit="return validateForm();" method ="post">

I understand that I can now use the variables I passed alone from one.php in two.php... 我知道我现在可以在two.php中使用从one.php单独传递的变量...

My question is, how would I use the variables from one.php in three.php? 我的问题是,我如何在Three.php中使用one.php中的变量?

I keep getting "undefined index" errors every time I try to call them as I would in two.php. 每当我尝试像在two.php中一样调用它们时,都会不断出现“未定义索引”错误。

Thank you very much for your help! 非常感谢您的帮助!

Edit: I should have mentioned that I am using Sessions as well. 编辑:我应该提到我也在使用Sessions。

On the second page, I do something like this: 在第二页上,我执行以下操作:

$_SESSION['name'] = $_POST['name']; $ _SESSION ['name'] = $ _POST ['name'];

and it lets me pass on the name gathered from that first page to the second one, but when I attempt to do this on the third page, it just says "undefined index". 它使我可以将从第一页收集的名称传递给第二页,但是当我尝试在第三页进行操作时,它只会显示“未定义的索引”。

Instead of using post, you would need to use $_SESSION 除了使用post之外,您还需要使用$ _SESSION

so for example: 因此,例如:

one.php 一个.php

session_start();
$_SESSION["mypostvalue"] = $_POST["mypostvalue"];
$_SESSION["mypostvalue1"] = $_POST["mypostvalue1"];

three.php three.php

session_start();
echo $_SESSION["mypostvalue"];
echo "<br />";
echo $_SESSION["mypostvalue1"];

Then once you are done with the session data, you need to get rid of it like this: 然后,一旦完成会话数据,就需要像这样摆脱它:

session_start();
$_SESSION = null;
session_destory();

Note Only use session_start() once per page, and before any data is sent to the browser such as html or white space. 注意:每页只能使用一次session_start(),并且在将任何数据发送到浏览器之前,例如html或空白。

I understand that I can now use the variables I passed alone from one.php in two.php... 我知道我现在可以在two.php中使用从one.php单独传递的变量...

Not as such. 不是这样的。

Successful form controls in the form will submit their values to two.php . 表单中成功的表单控件会将其值提交到two.php You can then access them via $_POST['name_of_control'] . 然后,您可以通过$_POST['name_of_control']访问它们。

If you generate form controls (possibly of type hidden) with values created from PHP variables (making sure to escape them to defend against XSS attacks) then they will appear in the form and be submitted. 如果您使用从PHP变量创建的值生成表单控件(可能是隐藏的类型)(确保对其进行转义以防御XSS攻击),则它们将出现在表单中并提交。

My question is, how would I use the variables from one.php in three.php? 我的问题是,我如何在Three.php中使用one.php中的变量?

That depends on how the browser gets to three.php. 这取决于浏览器如何访问three.php。 You could generate another form and put the values in hidden inputs again. 您可以生成另一种形式,然后将值再次放入隐藏的输入中。

$ _POST是每个请求,您应该使用$ _SESSION或$ _GET将它们传递到第三个文件。

Just make sure your input elements are named correctly. 只要确保您的输入元素命名正确。 If you want to use $_POST["myvar"] in two.php , then the name attribute in one.php needs to be "myvar". 如果您想使用$_POST["myvar"]two.php,那么name属性在one.php需要“MYVAR”。

<form action="two.php" method="post">
    <input type='text' value='test myvar' name='myvar' />
    <input type='submit' value='go to 2' />
</form>

To use the variables from one.php in three.php , just pass them through as hidden inputs. 要使用从three.php one.php变量,只是通过他们通过为隐藏输入。 That is, echo them back to the form in two.php : 也就是说,将它们回显到two.php中的形式:

<form action="three.php" method="post">
    <input type='text' value='test var 2' name='myvar2' />

    <?php echo "<input type='hidden' name='myvar' value='" . $_POST["myvar"] . "' />"; ?>

    <input type='submit' value='go to 3' />
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM