A. Appleby A. Appleby - 10 months ago 53
Ajax Question

ajax() considering value null jQuery

I am trying to get all input data from a

and send it for processing (in PHP). However, even though I am certain, as I have console logged the variable
before calling the
function, there is a value.

$("#finalsubmit").click(function () {
var bookeeDetails = [];

$("#bookeeDetails :input").each(function () {
bookeeDetails[$(this).attr('name')] = $(this).val();


url: url,
type: 'post',
data: {
bookee: bookeeDetails
success: function (response) {

Basically, I'm going for having access to
in the PHP script. Unfortunately, when the request is being sent nothing is being received in PHP. I have tested putting in fixed values, and that works, and I am 100% certain
contains data.

Is this a case of
not being populated yet as the request is being sent before the populating loop completes? If so, how do I stop this from happening?

Answer Source

Since i see submit in finalsubmit i guess that you've a form there so probably you're looking for serializeArray:

$( '#bookeeDetails' ).serializeArray();

Hope this helps.

var data = $( '#bookeeDetails' ).serializeArray();

var books = {};

$.each(data, function(i, obj){
  books[obj.name] = obj.value;  

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form id='bookeeDetails'>
  <input type="text" name="title" value="Biography">
  <input type="text" name="autor" value="Jack">
  <input type="text" name="edition" value="2016">