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?
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.
Can anyone give me guidance on how to pass an Object from one page to another using the $_POST array? 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);
is O:6:
, which I know is incorrect since the object has properties holding a player's name, number, health, strength, etc.
The 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.