i have a little problem with my program and not so experienced in php. I want to transfer an array of objects with a html form to my php file. First i will let you take a look on my code:
location.php (with executable forms)
<form action="action/add_items.php" method="POST">
<table border="1" width="20%">
<thead>
<th>
<?= $lang["location_task"][$location->getProperty("location_id")][$location_task->getProperty("location_task_id")]; ?>
</th>
</thead>
<tbody>
<?php
$location_task_items = $location_task->getProperty("location_task_items");
foreach ($location_task_items as $location_task_item) {
?>
<tr>
<td>
<?= $lang["item"][$location_task_item->getProperty("location_task_item_id")]; ?>
</td>
<td>
<?= $location_task_item->getProperty("location_task_item_value"); ?>
</td>
</tr>
<?php
}
?>
<tr>
<td></td>
<td>
<input type="hidden" value="<?php print_r($location_task_items); ?>" name="location_task_items"/>
<input type="submit" value="start"/>
</td>
</tr>
</tbody>
</table>
</form>
You can see that i only print the array in the input hidden value. Is that right?
add_items.php
$location_task_items = $_REQUEST["location_task_items"];
foreach($location_task_items as $location_task_item) {
if($Player_Has_ItemsClass->getObjectByIds($player_id, $location_task_item->getProperty("location_task_item_id")) == null) {
$player_has_items_attributes = array();
$player_has_items_attributes["player_has_items_player_id"] = $player_id;
$player_has_items_attributes["player_has_items_item_id"] = $location_task_item->getProperty("location_task_item_id");
$player_has_items_attributes["player_has_items_item_value"] = $location_task_item->getProperty("location_task_item_value");
$player_has_items = new Player_Has_Items($player_has_items_attributes);
$Player_Has_ItemsClass->insertObject($player_has_items);
} else {
}
}
I only get the array as string and this exception on the foreach in add_items.php:
Invalid argument supplied for foreach()
I also tried it with json_encode and json_decode:
print_r(json_encode($location_task_items))
json_decode($_REQUEST["location_task_items"]);
but only get (object attributes were public):
Call to undefined method stdClass::getProperty()
Thanks for you help :)
In your hidden field use json_encode
and then in your destination use json_decode:
<input `type="hidden" value='<?php echo json_encode($location_task_items); ?>' name="location_task_items"/>`
You could do this
<input type="hidden" value="<?php echo serialize($location_task_items); ?>" name="location_task_items"/>
and then
$location_task_items = unserialize($_REQUEST["location_task_items"]);