[英]How do I pass an Object between Web Pages through POST in PHP?
Is it possible to pass an Object through a Hidden Field in an HTML Form using $_POST and retrieve that Object on the page that the form links to? 是否可以使用$ _POST通过HTML表单中的隐藏字段传递对象,并在表单链接到的页面上检索该对象?
On the first page, I have a form like the one below: 在第一页上,我有一个类似以下的表格:
<?php
session_start();
require_once '../Model/player.php'; // To Enable Creation of a New Player Object
$playerName = filter_input(INPUT_POST, 'playerName');
$playerNumber = 1;
$player = new player($playerName, $playerNumber);
if (isset($player))
{
echo '<p>Successfully created your player!</p><br>';
?>
<form class="viewStats" action="../View/displayPlayerStatsView.php" method="post">
<input type="hidden" name="playerObject" value="<?php echo $player; ?>">
<input type="submit" value="View Your Player's Stats">
</form>
<?php
}
?>
And on the second (receiving) page, I have code like the code below: 在第二个(接收)页面上,我有类似以下代码的代码:
session_start();
require_once '../Model/player.php'; // To Use Player Object
$player = filter_input(INPUT_POST, 'playerObject'); // ERROR: Thinks the Player Object is a string.
My error seems to be that the receiving page that retrieves the 'playerObject' from the $_POST array is acting like the Object is a string. 我的错误似乎是从$ _POST数组检索“ playerObject”的接收页面的行为就像Object是一个字符串。
Can anyone give me guidance on how to pass an Object from one page to another using the $_POST array? 谁能给我有关如何使用$ _POST数组将对象从一页传递到另一页的指导? Is this even possible? 这有可能吗? Thank you in advance. 先感谢您。
UPDATE: Based on suggestions to serialize the Object, I now am getting the following errors: 更新:根据序列化对象的建议,我现在收到以下错误:
If I change my code on the first (sending) page to: 如果我将第一页(发送)上的代码更改为:
$playerSerial = serialize((object) $player);
<form class="viewStats" action="../View/displayPlayerStatsView.php" method="post">
<input type="hidden" name="playerObject" value="<?php echo $playerSerial; ?>">
<input type="submit" value="View Your Player's Stats">
</form>
and change the code on the second (receiving) page to: 并将第二个(接收)页面上的代码更改为:
$playerSerial = filter_input(INPUT_POST, 'playerObject');
print_r($playerSerial);
$player = unserialize($playerSerial);
then the output I get from print_r($playerSerial);
然后我从print_r($playerSerial);
得到的输出print_r($playerSerial);
is O:6:
, which I know is incorrect since the object has properties holding a player's name, number, health, strength, etc. 是O:6:
:,我知道这是不正确的,因为该对象的属性包含玩家的姓名,号码,健康状况,力量等。
The require_once '../Model/player.php';
require_once '../Model/player.php';
code exists in both PHP files, and it comes right at the top of both before any other code is executed. 两个PHP文件中都存在代码,并且在执行任何其他代码之前,代码都位于两个文件的顶部。
You have to make a few additions and corrections: 您必须进行一些补充和更正:
<?php
//... your previous code
$player = serialize($player);
?>
<form class="viewStats" action="../View/displayPlayerStatsView.php" method="post">
<input type="hidden" name="playerObject" value="<?php echo $player; ?>">
<input type="submit" value="View Your Player's Stats">
</form>
Use the serialize() function to create a string that can be passed to your other page which you can unserialize() as follows: 使用serialize()函数创建一个字符串,该字符串可以传递到另一个页面,您可以按如下所示将其反序列化:
secondPage.php: secondPage.php:
$player = $_POST['playerObject'];
$player = unserialize($player);
Also, you forgot to use echo here: 另外,您忘记了在此处使用echo:
change 更改
value="<?php $player ?>"
to 至
value="<?php echo $player; ?>"
Get yourself into serialization: the process of making a string from the object, which can in future be deserialized from the string back to object. 着手进行序列化:从对象生成字符串的过程,将来可以从字符串反序列化回对象。
Some docs, which could be useful for you: 一些文档,可能对您有用:
PHP - How object serialize/unserialize works? PHP-对象序列化/反序列化如何工作?
http://php.net/manual/ru/oop4.serialization.php http://php.net/manual/ru/oop4.serialization.php
http://www.phpinternalsbook.com/classes_objects/serialization.html http://www.phpinternalsbook.com/classes_objects/serialization.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.