Save sort order to hidden field with sortable jQuery plugin

Posted on 2014-03-28
Last Modified: 2014-03-29
I am using this plugin ( to create a feature for a website where a user can add form fields on the fly.

The idea is that they will give the form a name (title), and then add a series for form fields. These form fields need to be drag and drop sortable (thus the plugin) so the user can rearrange the order.

Once they are satisfied with the order, they will click the "Create Form" button, and the data to create the form will be submitted.

The data itself will be stored in three tables. Table 1: Forms, Table 2: Fields, and Table 3: Forms_Has_fields. The Forms_Has_fields table will includes a sort_order column, which tells the system in what order to display the fields.

That's where this question is important: when a field is re-sorted using the drag and drop feature, I need a way to save the sort order in an <input type="hidden"> or some other form control so I can parse it with PHP and create the fields properly.

If this was in PHP, I would just keep everything in an associative array: ($fieldname => $sortorder), but that doesn't seem like it's an option in javascript. Ideally, I could keep this in an array in Javascript, and then when sumit was clicked, dump that array as a JSON to an <input> field, and POST it to PHP. But if that's possible, I don't know how to do it.

If there is another (more elegant) way to handle this, I am open to suggestions.
Question by:DrDamnit
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 75

Accepted Solution

käµfm³d   👽 earned 500 total points
ID: 39962078
Since any object in Javascript can be defined as a string, why not use something like serializeArray to convert your array into a string that can be put into the hidden field. It will be posted back to the server, but you won't be doing anything with it--just dumping it back out to the hidden field in the response. On page load, you can call parseJson to get the data back into an array.
LVL 32

Author Comment

ID: 39962090
But here's my hangup: when I change the sort order, it will obviously fire an event... is the array still available after the page loads and the $(document).ready() has finished? Keep in mind, I am adding a DOM / form element (I'm using <li data-name="foo">foo</li>).
LVL 32

Author Comment

ID: 39962100
Additionally... I want to re-generate the <input> data every time. I can sift, sort, push and pop the array, but dumping to JSON every time will make it clean.

Should this array be a global variable? (I hate globals, but is this the best way?)
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39962105
If you keep it in a global variable (i.e. not declared locally to any particular function [or event]), then it should be. Obviously you'll have to update it as the sort order is modified.
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39962109
Feel free to wait for one of the web-dev gurus to pop in. There may be a more elegant way to handle this; this is just what popped in my head upon reading your question.
LVL 32

Author Comment

ID: 39964085
Turns out, I was making it more complicated than it needed to be. I am giving you the points because you answered my question; however I did find a more elegant way of doing it. The <ul> is, in fact, the order and array already. There is no reason to create a separate array to manage what is being saved in what order because the <ul> does it already. All you have to do is itterate through it.

And, best of all, I didn't have to use global variables.
function buildOrder() {
	var buffer = [];
	//Rebuild the sort order array.
	$("#theFields li").each(function(index){
		var fieldName = $("#fieldName").val();
		$("#theFields").append("<li><div class=\"form-group\"><label>"+fieldName+"</label><input type=\"text\" class=\"form-control\"></div></li>");
		$(".sortable").sortable().bind('sortupdate',function() {

Open in new window

LVL 32

Author Closing Comment

ID: 39964089
Oh, and I am using serializeArray. It was the other bit that was more elegant.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question