0

I am trying to pass a serialzed array via ajax request to my saveData.php file in order to save the data into my database.

var postData = $('#formular').serializeArray();

this is the Data I want to pass. I am creating an array with all the data in order to pass it as a json-Array to the php file:

    var formArray={};
    $.each(postData, function (index, field ){
        formArray[field.name]=field.value;
    });
    var formData=JSON.stringify(formArray);

$.ajax({
        type: 'POST',
        contentType: 'application/json',
        url:"functions/saveData.php",
        data : formData,
    }).done(function(data) {
        console.log('done: '+data);
    }).fail(function(data) {
        console.log('fail: '+data);         
    });

When I look at the $_POST in PHP, there is an emty array ...

on the php side I try to catch the data with the following code....

$getPostedData=$_POST;

if($debug){
    echo '<pre>';
    print_r($getPostedData);
    echo'</pre>';
}

This gives me the following Output:

    Array
(
)

what am I doing wrong? Thanks for helping me out.

1 Answer 1

0

Simple example, which similar to your case. It's enough to use serialize():

var postData = $('#formular').serialize();

And now you can simply send it to AJAX:

$.ajax({
   url:"functions/saveData.php", 
   method:"POST",
   data: postData
}).done(function(resp){
   console.log(resp);
});

HTML code form looks like next:

<form id="formular" onsubmit="javascript:return false;">
    <!-- for defined data -->
    <input type="hidden" name="order_id" value="3435" /> 

    <!-- for arrays -->
    <input type="text" name="ex_arr[]" value="3343" />
    <input type="text" name="ex_arr[]" value="1123" />

</form>
Sign up to request clarification or add additional context in comments.

4 Comments

Concatenation console.log('done: '+data); means string + array/object. Do just console.log(data);. Also, present your HTML form.
@ccalbow, you're welcome) never forget datatypes of input/output variables
the problem was not the output format, but the empty output... now it gives me all the data I needed in the php array. It is full of data now. one of the problems was the content type. I deleted it and now it works! Thanks a lot!
Yeah, if you don't need to send files to the PHP, it's enough to use these 3 lines, nothing else.

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.