I want to send form inputs from a page to a PHP function, using AJAX, and save the data in a multidimensional array. There should be no page reload, and the response can be a simple true or false. In the Chrome browser, I can see both inputs in the "Request Payload" element, in JSON, but how can I retrieve the data in PHP and transform this data into an array?
HTML:
<div id="formEditBilling"><form>
<input name="register[personal][firstname]" type="text" id="firstname" value="" class="text required " placeholder="Firstname">
<input name="register[personal][lastname]" type="text" id="lastname" value="" class="text required " placeholder="Lastname">
<input type="buttom" id="editBilling" value="speichern"/></form></div>
JavaScript:
<script type="text/javascript">
$('#editBilling').click(function() {
editBilling();
});
function editBilling(){
var url = '/checkout/saveAddress'; // Server Function
var data = $('#formEditBilling form').serializeArray();
data = JSON.stringify(data);
$.ajax({
'type': 'post',
'async': false,
'data': data,
'url': url,
'contentType': "application/json",
'dataType': "text",
'success': function (result, data) {
$('.output').html(data); // Output Div
}
});
}
</script>
Here's my problem. How can I get a array like:
['register'] => Array
(
['personal'] => Array
(
['firstname'] => "Michael"
['lastname'] => "Cooper"
)
PHP: Here's my attempt, but it's not working, $_POST seems to be empty.
public function saveAddressAction(){
$data = json_decode(stripslashes($_POST['register']));
return true;
}