I want to serialize a form into json, process the json object, then reserialize it to send to a php script via ajax.
Here is a rough example of what I want to do:
s = $('.dia_req_form').serialize();
j = //convert s to json ...HOW??
if(j.name)
{
alert("you must enter a name");
}
if(selectedID)
{
j.id = selectedID;
}
s = //serialize j ...HOW??
You can see the 2 parts that say HOW??
You can use
.serializeArray()and$.param()like this:Internally,
.serialize()is really equivalent to$.param($(this).serializeArray()), so all this is doing is breaking the steps apart, adding an item if needed..serializeArray()is an array of objects with 2 properties (nameandvalue), all we’re doing is adding some object to the array if needed, then calling$.param()to make it a string.