One more challenge I’ve had these days, I wanted to have a way to define my forms in xml format and render them at run time on the web page. And on web page I want to have just content place holders, so if I work together with web designer, he can jut define the place where the will be rendered.
So to skip all unnecessary introduction, let me show you how whole thing works.
My form place holder looks like this:
<div class="jform">contactform.xml</div>
As you can see I just added a div that has class jqform (I used short for jQuery forms). In text section of this content container I've put the name of the file I will use, the one that contains my XML form definition.
My contactform.xml definition file of the form is following:
<form title="Contact Us" url="save.aspx" submitLabel="Send" replyCallback="__formSubmited">
<formItem title="Name">
<input type="text" id="name" />
</formItem>
<formItem title="Email">
<input type="text" id="email" />
</formItem>
<formItem title="Message">
<input type="text" multiline="yes" id="message" />
</formItem>
</form>
As you can see this form has a title, it has an URL where I will submit form, it has also submitLabel field that contains a text of the Submit button, the last parameter you see there is replyCallback which the name of the function I will use to pass result of the data post, please note that data i pass to this function is in escaped format.
If url is not specified it will render just form, without any submit buttons and form post functionality included.
And after rendering this form would look like this:
Now, to make things working as I need we going to add into head section of our html the following:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://jqcommons.googlecode.com/svn/trunk/jqc.core.js"></script>
<script src="jqforms.js" type="text/javascript"></script>
As you can see I use jQuery library hosted by Google and second reference is to jQuery Commons, my recent contribution for webware developers. This lib contains set of functions that makes my life easier, like string operations, date formatting and so on, bunch of goodies.
As you recall, above I mentioned replyCallback function, let’s make it simple:
<script type="text/javascript">
function __formSubmited(reply) {
alert(unescape(reply));
}
</script>
As I told you string comes in escaped format, so to work with this string I need to unescape it back.
The whole magic done actually by third JavaScript file that I added to my HTML, you can see it referred as jqforms.js. So what it does, is on document_ready event it checks for jqform class enabled containers and renders a form out of XML definition you referred within a container.
You can see whole thing in action at http://labs.skitsanos.com/demos/jsforms/ and ‘jQuery Forms Player’ (jqforms.js) prototype you can get from http://labs.skitsanos.com/demos/jsforms/jqforms.js
Now this thing saves me unreal amount of time, hope it will for you as well.
Follow us on Twitter