jQuery Form Builder Plugin

Trellis Development (a parent company of web-based products which I co-founded) has been developing a custom content management system which needed a form creation tool. I adapted a form builder that I created for a previous project as a jQuery 1.3 plugin. It loads in existing form structure data through an XML file (which would be generated on the server) and passes the changes as a serialized array back to the server.

View the Demonstration
Get source from github

I’ve forked the code from the cms to serve as a stand-alone plugin. It’s extremely easy to setup, as all you need to do is to activate it on an un/ordered list item element. Then, write your backend code to handle the incoming array as you need, and output the xml data for when the form loads.

<ul id="form-builder"></ul>
$(document).ready(function(){
	$('#form-builder').formbuilder({
		'save_url': 'save.php',
		'load_url': 'form-a.xml'
	});
});

The save_url is the url that the ajax will be sent to when the user saves the form. The form information is serialized so that the backend programming may handle it as an array.

The load_url is the url of an xml file that describes any existing form information, and the system uses it to restore the fields.

This requires jQuery 1.3+ and uses the scrollTo plugin for nice scrolling.

This is the first revision that’s external to our cms so I’ve labeled it 0.1.

jQuery Serialize List Plugin

During several recent projects I needed the ability to serialize an unordered list and pass it back to the server. A google search didn’t reveal much and I didn’t want to go digging this functionality out of larger plugins like sortable, UI, etc.

I decided to wrap it all up into a jQuery plugin so that it’s easier to use in different projects. The serialized un/ordered list may be sent back to the server, and for example in PHP, will be interpreted as an array.

You may use this plugin by simply calling it on your UL or OL.

$(document).ready(function(){
    $('ul').serializelist();
});

And below, is the plugin source. It requires jQuery 1.2+.

(function($){
    $.fn.serializelist = function(options) {
        /**
         * jQuery Serialize List
         * Copyright (c) 2009 Mike Botsko, Botsko.net LLC
         * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
         * Copyright notice and license must remain intact for legal use
         * Version 1
         *
         * Serialize an unordered or ordered list item. Optional ability
         * to determine which attributes are included. The serialization
         * will be read by PHP as a multidimensional array which you may
         * use for saving state.
         */

        // Extend the configuration options with user-provided
        var defaults = {
            prepend: 'ul',
            is_child: false,
            attributes: ['id', 'class']
        };
        var opts = $.extend(defaults, options);
        var serialStr     = '';

        if(!opts.is_child){ opts.prepend = '&'+opts.prepend; }

        // Begin the core plugin
        this.each(function() {
            var ul_obj = this;

            var li_count     = 0;
            $(this).children().each(function(){

                for(att in opts.attributes){
                    serialStr += opts.prepend+'['+li_count+']['+opts.attributes[att]+']='+$(this).attr(opts.attributes[att]);
                }

                // append any children elements
                var child_base = opts.prepend+'['+li_count+'][children]';
                $(this).children().each(function(){
                    if(this.tagName == 'UL' || this.tagName == 'OL'){
                        serialStr += $(this).serializelist({'prepend': child_base, 'is_child': true});
                    }
                });
                li_count++;
            });
        });
        return(serialStr);
    };
})(jQuery);

MySQL Backup Shell Script

Many of my clients need a quick backup solution before we implement anything on a larger more permanent scale. I wrote this simple script that simply make a copy of a database and then created a timestamped tarball of the file.

#!bin/sh

date=`date +%Y-%m-%d_%Hh%M`

cd /my/path/for/backups/

mysqldump -umy_user -p"mypass" my_database > $date.sql

tar -zcvf $date.tgz *.sql

# uncomment this line to import the sql into another database, for example as a mirror
#mysql -umy_user -p"mypass" mirror_database < $date.sql

rm *.sql

Then, just setup a cron job to run this at whichever interval you want this to run.