我的程序有点问题,而且在php中没那么有经验。我想将带有html表单的对象数组传输到我的php文件中。首先,我会让你看看我的代码:
location.php(可执行表格)
<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>
你可以看到我只在输入隐藏值中打印数组。是吗?
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 {
}
}
我只将数组作为字符串,并在add_items.php中的foreach上显示此异常:
为foreach()提供的参数无效
我也尝试过json_encode和json_decode:
print_r(json_encode($location_task_items))
json_decode($_REQUEST["location_task_items"]);
但只获取(对象属性是公共的):
调用未定义的方法stdClass :: getProperty()
谢谢你的帮助:)
答案 0 :(得分:0)
你可以这样做
<input type="hidden" value="<?php echo serialize($location_task_items); ?>" name="location_task_items"/>
然后
$location_task_items = unserialize($_REQUEST["location_task_items"]);
答案 1 :(得分:0)
在隐藏字段中使用json_encode
,然后在目的地中使用json_decode:
<input `type="hidden" value='<?php echo json_encode($location_task_items); ?>' name="location_task_items"/>`