首页 文章

无法使用html格式传输对象数组

提问于
浏览
1

我的程序有点问题,而且在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"]);

但只有get(对象属性是公共的):

调用未定义的方法stdClass :: getProperty()

谢谢你的帮助:)

2 回答

  • 0

    在隐藏字段中使用 json_encode 然后在目的地中使用json_decode

    <input `type="hidden" value='<?php echo json_encode($location_task_items); ?>' name="location_task_items"/>`
    
  • 0

    你可以做到这一点

    <input type="hidden" value="<?php echo serialize($location_task_items); ?>" name="location_task_items"/>
    

    然后

    $location_task_items = unserialize($_REQUEST["location_task_items"]);
    

相关问题