Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read from serialize to populate form

I did serialize() on my form and saved the string, is there any function which can populate values back to the form from serialized string?

like image 678
Firdous Avatar asked Jan 27 '12 15:01

Firdous


2 Answers

Check out http://phpjs.org/functions/unserialize:571

I recommend instead of serializing data for communication with javascript, you use JSON. PHP should have json_encode() and json_decode() to help with this, and javascript also has built in JSON handling functions, which you may not even need. For example, if $.getJSON gets a valid JSON string from the server, it will be transformed into a javascript object automatically.

EDIT: assuming you are talking about jQuery's $.serialize(), that I know of there's no function to undo this (I'm not even sure why that would ever be necessary..) but this should work:

$.each(serialized.split('&'), function (index, elem) {
   var vals = elem.split('=');
   $("[name='" + vals[0] + "']").val(vals[1]);
});
like image 139
Explosion Pills Avatar answered Oct 03 '22 12:10

Explosion Pills


Here is the updated version of Explosion Pills' answer with the additional suggestions in the comments applied:

$.each(serialized.split('&'), function (index, elem) {
   var vals = elem.split('=');
   $("[name='" + vals[0] + "']").val(decodeURIComponent(vals[1].replace(/\+/g, ' ')));
});
like image 20
Devon Avatar answered Oct 03 '22 12:10

Devon