0

I'm trying to submit a form and get the data stocked in it. So, in javascript, I have something like :

$(".form_itk").submit(function(event){

    var formData = $(this).serializeArray();
    //...other variables

    $.post("/kohana-v3.3.5/ajax/nouvel_itk",{itk : formData, id_culture : id_culture, id_sol : id_sol, id_region : id_region}
        ).done(function(data){
            console.log(data);
        })
}

Then, the variable formData is sent with ajax to make an insert request. The problem is that I can have empty fields in my form but in that case, I want to get a "null" value. Currently, I get nothing, even if the field doesn't exist. How can I force the value to be "null" ? Or is there an other way to get the data of a form in javascript ?

Thanks in advance !

EDIT :

Let's say I have a simple form in html :

name : foo

age : 25

sex : woman

hobbie : (empty)

I want to add these data into a database.

So I run my javascript code to get the values of this form and after the serializeArray(), I get something like :

0 : Object : foo

1 : Object : 25

2 : Object : woman

And that's it ! The "hobbie" field doesn't exist in this array.

After that, when I try to do a database request :

Insert into 'human' set('name','age','sex','hobbies') values('foo','25','woman')

This request can't work, that's why I want to have a "null" instead of nothing.

Erlaunis
  • 1,433
  • 6
  • 32
  • 50
  • Can you show us your request handler? – Strah Behry Apr 29 '16 at 14:27
  • @StrahBehry I edited with the ajax request – Erlaunis Apr 29 '16 at 14:28
  • Do you need `null` because that is how it is supposed to be stored in the database or are you using it elsewhere? – RST Apr 29 '16 at 14:37
  • @RST I need `null` because in my database request, I need all the data to match with the column names. Without all the data, the request can't work. – Erlaunis Apr 29 '16 at 14:41
  • you can get the implementation of the serializeArray and override. check this link http://stackoverflow.com/questions/22368876/jquery-input-serializearray-function-using-native-api – Hugo S. Mendes Apr 29 '16 at 14:43
  • I may be misunderstanding the issue but it seems you are overcomplicating things. The script that handles the query can just check what is available and add the missing ones and set them to `null`, and the empty ones too if that applies. Why would you send `null` values when the query script can create/handle them? – RST Apr 29 '16 at 14:46
  • @RST When I check the values in `formData` before doing the ajax query, there is already null values. The function `serializeArray()`. It's the values of the `formData` which are empty, not the data that I send in ajax. Actually, the script doesn't create the `null` values, that's why I'm asking. – Erlaunis Apr 29 '16 at 14:53
  • @RST I edited my post to better understanding. – Erlaunis Apr 29 '16 at 15:02
  • Where is your database query performed? I assumed it is a PHP script. – RST Apr 29 '16 at 20:28

1 Answers1

0

To use JQuery serializeArray(), your input must contain name attribute.

the element cannot be disabled and must contain a name attribute https://api.jquery.com/serializeArray/

Mohammad
  • 21,175
  • 15
  • 55
  • 84
  • I think that it's the function `serializeArray()` which replace the empty field with nothing. If I do that, I just loop though an array which can't have empty value. – Erlaunis Apr 29 '16 at 14:48
  • @Erlaunis If your problem solved and my answer were useful, please accept it. – Mohammad May 16 '16 at 09:01