首页 > 解决方案 > 从json数据中获取php页面中的数据

问题描述

我有一个 HTML 表单,我将其转换为 JSON 并将其传递到另一个页面save.php。问题是我看不到save.php页面上的数据。

我的 HTML 表单代码:

<form id="my-profile" action="save.php">
    <div id="accordion" style="width:80%; margin:0 auto;background:">
        
        <div class="s_panel">
            <div>
                <h3 style="width:100% ;background: gray;margin-top:5px">
                    <input type="text" class="TEER" maxlength="20" id="item" style="height:35px;width:20%;padding:5px;" placeholder="Enter title , ex (basic information) " name="sec4" value="Works">
                    <input class="ed" type="button" style="float:right;height:35px;width:100px;" value="Edit">
                </h3>
                <textarea name="sec4[asd]" style="height:200px"></textarea>
            </div>
        </div>
    </div>

</form>

<script>
    $(function() {
        $("#accordion").accordion({
            collapsible: true,
            active: false,
            height: 'fill',
            header: "h3",
            update: function(event, ui) {
                var order = $("#accordion").sortable("toArray");
                $('#image_order').val(order.join(","));
                alert($('#image_order').val());
            }
        }).sortable({
            items: '.s_panel'
        });
    });

</script>

<script type="text/javascript">
    $(function ahmed() {
        $("#btnSerialize").on("click", function() {

            var obj = $('form').serializeJSON();
            console.log(obj);

            var jsonString = JSON.stringify(obj);
            $("#result").val(jsonString);
            var xht = new XMLHttpRequest();
            xht.open("POST", "save.php");
            xht.setRequestHeader("Content-Type", "Application/json");
            xht.send(jsonString);

        })
    });

</script>

save.php我使用以下代码:

$json=file_get_contents('php://input');

我无法打印save.php页面上的内容。

我错过了什么?如何在save.php文件中显示 HTML 表单的输出?

标签: javascriptphpjson

解决方案


推荐阅读