dongxingji3882 2016-10-19 14:58
浏览 29
已采纳

如何从php重定向中序列化ajax中的$ _POST数据

I am having users fill in a form on page A, I then redirect to page B sending $_POST data with it. I've seen that I can serialize post data from a form like this $("form").serialize() or this.serialize() but I don't want to retrieve data from a form I need to get it from $_POST. How do I go about serializing $_POST data not from a form that exists on the current page?

  • 写回答

2条回答 默认 最新

  • douxianliu6756 2016-10-25 18:33
    关注

    I found a solution to this in case anyone else ever wonders how to do this. var post_info = []; creates an array to hold all of the $_POST variables. This is what takes all the post variables and puts them into a json array for ajax post_info = <?php echo json_encode($_POST); ?>;. And the ajax looks like normal.

                "ajax": {
                    "url": "ajax/test.php",
                    "type": "POST",
                    "data": {post_info: post_info}
                },
    

    On the php side I didn't need to json_decode because ajax sends this as a $_POST variable. Except it is now a multidimensional array so accessing variables looks like this $_POST['post']['item']. And there you go. I've never answered my own question before.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?