Analyzing the following line:
$.post("autosave.php", {'meus_dados': dados});
The Form Data
HTTP request will be sent as:
meus_data:title=sr&body=test
That is, in PHP will only be available the $_POST['meus_dados']
which will be a string with the value title=sr&body=teste
not an array as expected. Test in PHP print_r($_POST)
and see the array structure $_POST
.
A solution will be to use the function return serializeArray
to create a new object:
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
Applying the solution:
var dados = $('#form').serializeObject();
$.post("autosave.php", {'meus_dados': dados});
So the Form Data
HTTP request will have the following format:
meus_data[title]:sr
meus_data[body]:test
In PHP the array $_POST
will have the necessary indexes for your script below work:
$title = &$_POST['meus_dados']['title'];
$body = &$_POST['meus_dados']['body'];
Source (important also read comments): https://stackoverflow.com/a/1186309/3636960
Is there a title key? Gave a print_r to be sure?
– rray
Yeah, sure.
title=titulo&body=corpo
@rray– lucasbento
The
Form Data
in the latter case it will be sent as:meus_dados:title=sr&body=teste
, that is, in PHP will only be available$_POST['meus_dados']
which will be a string with the valuetitle=sr&body=teste
not an array as expected. Test in PHPprint_r($_POST['meus_dados'])
.– Filipe Moraes
Yes @Filipemoraes, but I need to take these values individually by reference.
– lucasbento
pq needs to be by reference?
– rray
rray, because theoretically, only so that I am able to obtain the expected result.
– lucasbento