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.