Jump to content

Converting Javascript Array data to PHP using POST?


galvin

Recommended Posts

On one page, I have a javascript array that gets created while a user is interacting with some JQuery and the page has a submit form.  When done interacting with the JQuery,  the user submits the form.  And I want to post the data in the javascript array to another page with a PHP script, for processing via PHP. 

 

What is the best way to do this?  I googled it and I am seeing A LOT of differing opinions (JSON, serialize/unserialize, etc. etc) and I am not sure who to believe :)

 

Any insight would be appreciated.

 

Thanks!

 

Use JSON in the jQuery AJAX methods with "post" as the type. Then the $_POST array will be made up from the JSON keys.

 

var name    = $('input[name="name"]').val(),
    email   = $('input[name="email"]').val(),
    address = $('input[name="address"]').val();

$.ajax({
url: example.com,
data: { name: name, email : email, address : address },
type: "post"
});

 

$_POST will look like:

$_POST = Array
(
'name'    => 'bob',
'email'   => '[email protected]',
'address' => '123 someplace'
)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.