50% OFF!!!

Monday, July 11, 2022

JQuery serialize on form is EMPTY (ajax/post/get)

 Hi All,


I had several times a problem, that the collected data by jQuery API's serialize() or serializeArray() was empty, although the inputs were not empty.

Follow these checkups to by pass the problem.

1] when the input don't name attribute the result will be empty.

<form id="form1">
        <input type="text" value="test value">
        <button>submit</button>
</form>
//$('#form1').serialize()  ==> "" (empty)
 
 
SOLUTION:
<form id="form1">
        <input type="text" value="test value" value="ANYNAME">
        <button>submit</button>
</form>
//$('#form1').serialize()  ==> "ANYNAME=test+value" 
 
 
2] when the input is DISABLED the result will be empty.
 
<form id="form1">
    <input type="text" value="test value" name="ANYNAME" disabled="true" />
    <button>submit</button>
</form>
 
//$('#form1').serialize()  ==> "" (empty)
 
NOTE: sometimes the inputs are disabled upon form submit (loader shown), 
and while calling serialize/serializeArray the disabled inputs (selects/...) are ignored and empty values received.
 
 
 
3] make sure your form's HTML is well formatted. broken HTML code might hurt functionality.


HOPE HELPED HERE :)
 

No comments:

Post a Comment