1

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;
}
1
  • You have wrong input type on input "buttom" should be "button". And maybe you can take a look useful plugin for jquery - jquery-form (malsup.com/jquery/form) Commented Sep 24, 2013 at 16:15

2 Answers 2

1

Your function's brackets aren't joining up properly, try using this

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
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but this was only a copy & paste problem when I created my problem.
1

You need to transform your serialized formdata into correct JSON

var formdata = $('#formEditBilling form').serializeArray();

var formobject = {};

$(formdata).each(function (e) {
    formobject[formdata[e].name] = formdata[e].value;
});
var data = {
    action: action,
    json: JSON.stringify(formobject)
};

send with ajax

function send(data) {
    var deferred = $.ajax({
        method: "post",
        url: settings.ajaxURL,
        dataType: "json",
        data: data
    });
    return deferred.promise();
}

send(formdata).done(function(response){
    console.log(response)
}

catch with PHP

$_POST['action']
json_decode($_POST['json']);

1 Comment

$('#formEditBilling form').serializeArray(); working grate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.