Heinz Beck
Heinz Beck

Reputation: 11

Send form inputs with AJAX to PHP

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;
}

Upvotes: 1

Views: 1484

Answers (2)

Daniel
Daniel

Reputation: 4946

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']);

Upvotes: 1

Jamie Taylor
Jamie Taylor

Reputation: 4765

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
        }
    });
}

Upvotes: 1

Related Questions