无法传输 html 形式的对象数组
Can't transfer an object array with html form
我的程序有点问题,而且在 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 上出现此异常:
Invalid argument supplied for foreach()
我也用 json_encode 和 json_decode 试过了:
print_r(json_encode($location_task_items))
json_decode($_REQUEST["location_task_items"]);
但只能获取(对象属性是公开的):
Call to undefined method stdClass::getProperty()
感谢您的帮助:)
你可以这样做
<input type="hidden" value="<?php echo serialize($location_task_items); ?>" name="location_task_items"/>
然后
$location_task_items = unserialize($_REQUEST["location_task_items"]);
在您的隐藏字段中使用 json_encode
,然后在您的目标中使用 json_decode:
<input `type="hidden" value='<?php echo json_encode($location_task_items); ?>' name="location_task_items"/>`
我的程序有点问题,而且在 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 上出现此异常:
Invalid argument supplied for foreach()
我也用 json_encode 和 json_decode 试过了:
print_r(json_encode($location_task_items))
json_decode($_REQUEST["location_task_items"]);
但只能获取(对象属性是公开的):
Call to undefined method stdClass::getProperty()
感谢您的帮助:)
你可以这样做
<input type="hidden" value="<?php echo serialize($location_task_items); ?>" name="location_task_items"/>
然后
$location_task_items = unserialize($_REQUEST["location_task_items"]);
在您的隐藏字段中使用 json_encode
,然后在您的目标中使用 json_decode:
<input `type="hidden" value='<?php echo json_encode($location_task_items); ?>' name="location_task_items"/>`