Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery serialize question

Is there something else I can use on a form to get all the elements and their values that won't put them into URL notation? Or is there a quick way to clean up the string returned?

I'm getting this returned:

filePath=afile.doc&fileTitle=A+File&fileDescription=This+should+be+first+in+an+asc+sort%2C+last+in+a+desc

and would need to clean up the URL stuff for a database submission - (the "+" symbol nad %2C (comma).

Is there a simple way to reverse standard URL-encoded notation?

like image 360
PruitIgoe Avatar asked Dec 27 '22 22:12

PruitIgoe


2 Answers

Is there something else I can use on a form to get all the elements and their values that won't put them into URL notation?

Yes, you can use jQuery's serializeArray, which will "...Encode a set of form elements as an array of names and values." This would be best, as you avoid doing the encoding in the first place (I take it you want to work with the unencoded version of the values).

Example:

var a, index, entry;
a = $("#theForm").serializeArray();
for (index = 0; index < a.length; ++index) {
  entry = a[index];
  display("a[" + index + "]: " + entry.name + "=" + entry.value);
}

Live copy

Is there a simple way to reverse standard URL-encoded notation?

Yes. Individual encoded values can be decoded using decodeURIComponent; a full string of fields can be decoded with decodeURI.

like image 93
T.J. Crowder Avatar answered Jan 04 '23 22:01

T.J. Crowder


if you use post instead of query you can name your elements into an array

<input type="hidden" name="block[369][filepath]" value="somepath"/>
<input type="hidden" name="block[369][title]" value="somefile.gif"/>
<input type="hidden" name="block[369][description]" value="imagefile"/>

I'm kind of assuming allot here but this is how I put things into an array for php parsing so I don't have to rely on jQuery to serialize

just an idea

like image 35
mcgrailm Avatar answered Jan 04 '23 22:01

mcgrailm