moe moe - 1 year ago 123
jQuery Question

How do I serialize $_POST data in ajax from a php redirect

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

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?

moe moe
Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download