<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Chris Page - Software Engineer &#187; json</title>
	<atom:link href="http://www.chriswpage.com/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chriswpage.com</link>
	<description>... and student of Life, Philosophy, and Learning</description>
	<lastBuildDate>Sat, 17 Jul 2010 02:46:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Convert an XHTML form to a JSON object, then use jQuery&#8217;s AJAX methods to send the data to the backend</title>
		<link>http://www.chriswpage.com/2009/01/convert-an-xthml-form-to-a-json-object-then-use-jquerys-ajax-methods-to-send-the-data-to-the-backend/</link>
		<comments>http://www.chriswpage.com/2009/01/convert-an-xthml-form-to-a-json-object-then-use-jquerys-ajax-methods-to-send-the-data-to-the-backend/#comments</comments>
		<pubDate>Fri, 30 Jan 2009 02:19:00 +0000</pubDate>
		<dc:creator>cpage</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://www.chriswpage.com/2009/01/convert-an-xthml-form-to-a-json-object-then-use-jquerys-ajax-methods-to-send-the-data-to-the-backend/</guid>
		<description><![CDATA[I had a project at work that almost drove me insane tonight.  When it comes to Javascript, forget what you know about other languages, and get in the mode of:  &#8220;all objects are hashes&#8221;.
Case Scenario:
You have a form with lots of dynamically created input fields.  Instead of a normal submit button post, [...]]]></description>
			<content:encoded><![CDATA[<p>I had a project at work that almost drove me insane tonight.  When it comes to Javascript, forget what you know about other languages, and get in the mode of:  &#8220;all objects are hashes&#8221;.</p>
<h4>Case Scenario:</h4>
<p>You have a form with lots of dynamically created input fields.  Instead of a normal submit button post, you want to send the data to the backend using AJAX.</p>
<h4>Problem:</h4>
<p>After hours of Googling, you just can&#8217;t find information on dynamically populating a <a title="Javascript Object Notation" href="http://www.json.org" target="_blank">JSON</a> object.  You also want a clean and simple way to grab all that form data, put it in a JSON object, and then submit it with <a title="jQuery" href="http://www.jquery.com" target="_blank">jQuery</a>&#8217;s wonderfully simple AJAX methods.</p>
<h4>Example Solution:</h4>
<p><strong>XHTML from a dynamically generated form</strong><br />
I left some code out for simplicity, but just imagine it&#8217;s there.  The inputs below are added dynamically, and can be infinite.</p>
<pre>&lt;form id="invite_users"&gt;
    &lt;fieldset&gt;
        &lt;input name="users[0][name]" type="text"&gt;
        &lt;input name="users[0][email]" type="text"&gt;
    &lt;/fieldset&gt;
    &lt;fieldset&gt;
        &lt;input name="users[1][name]" type="text"&gt;
        &lt;input name="users[1][email]" type="text"&gt;
    &lt;/fieldset&gt;
    &lt;fieldset&gt;
        &lt;input name="users[1][name]" type="text"&gt;
        &lt;input name="users[1][email]" type="text"&gt;
    &lt;/fieldset&gt;

    &lt;!-- ...etc...etc.. --&gt;

    &lt;a href="javascript:void(null)" id="#submit_form"&gt;Submit&lt;/a&gt;
&lt;/form&gt;</pre>
<p><strong>jQuery Goodness</strong></p>
<p>Here is the sweet part.  In this example, we just iterate through all the input fields dynamically, and place them in a JSON object &#8212; which is what jQuery&#8217;s ajax method uses to send the data back to the server.</p>
<pre>$(document).ready(    function() {
    $("#submit_form").click(function() {
        var data = {}

        $("form#invite_users &gt; fieldset &gt; input").each(function() {
          data[$(this).attr("name")] = $(this).val();
        });

        $.post("post_form.php", data, function(returnData) {
          alert("Thank You!");
        });
    });
});</pre>
<p>Easy enough?</p>
<p>But wait!  There&#8217;s an even better way.  With the above example, we don&#8217;t account for complex element types passed in the request ( ie, radio boxes, check boxes, file inputs ).  Once again, jQuery simplifies things even further with <a title="jQuery's serializeArray Documentation" href="http://docs.jquery.com/Ajax/serializeArray" target="_blank">serializeArray()</a>:</p>
<pre>
    $("#submit_form").click(function() {
        var data = $("form#invite_users").serializeArray();

        $.post("post_form.php", data, function(returnData) {
            alert("Thank You!");
        });
    });
</pre>
<p>Hope this helps others out there!  Leave a comment on your thoughts!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chriswpage.com/2009/01/convert-an-xthml-form-to-a-json-object-then-use-jquerys-ajax-methods-to-send-the-data-to-the-backend/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
