我的程序有点问题,在PHP方面没有经验。我想将带有 html 表单的对象数组传输到我的 php 文件。首先,我将让您看一下我的代码:
位置.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()
感谢您的帮助:)
在隐藏字段中使用 json_encode
,然后在目标中使用 json_decode:
<input `type="hidden" value='<?php echo json_encode($location_task_items); ?>' name="location_task_items"/>`
你可以这样做
<input type="hidden" value="<?php echo serialize($location_task_items); ?>" name="location_task_items"/>
然后
$location_task_items = unserialize($_REQUEST["location_task_items"]);