The following code sends (I hope so) a Javascript object to a php file through Jquery's ajax call.
var emails = {};
emails.name = email;
.
.
$('button.gmail').click(function() {
$.ajax({ url: path,
data: emails,
type: 'post',
success: function(status) {
alert(status);
}
});
});
how can I examine this object in PHP and retrieve its keys/values?
2 Answers 2
This will encode the object into a application/x-www-form-urlencoded string, which will be available via $_POST in PHP.
So if your data object is something like:
var data = {
foo: 'bar',
baz: 10
};
Then in PHP you will have:
$_POST['foo'] //is "bar"
$_POST['baz'] //is 10
answered Sep 13, 2013 at 17:46
Chad
19.6k4 gold badges54 silver badges75 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
<?php
$foo = $_POST['foo']; //this will assign whatever 'foo' was assigned in the javascript POST request
Comments
default
application/x-www-form-urlencodedstring, which will be available via$_POSTin PHP.print_r($_POST)in your PHP file, and the alert will show whatever you received in the PHP file, which should be exactly the same as whatever you sent ?