<?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>gEt DoWn ToNiGhT &#187; jquery</title>
	<atom:link href="http://www.getdowntonight.co.uk/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.getdowntonight.co.uk</link>
	<description>amusingly digressive progressive discussions</description>
	<lastBuildDate>Mon, 05 Dec 2011 22:41:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Sample Page for sending JSON data as string to web-service</title>
		<link>http://www.getdowntonight.co.uk/2011/12/sample-page-for-sending-json-data-as-string-to-web-service/</link>
		<comments>http://www.getdowntonight.co.uk/2011/12/sample-page-for-sending-json-data-as-string-to-web-service/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 16:56:04 +0000</pubDate>
		<dc:creator>Sam</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[sample]]></category>

		<guid isPermaLink="false">http://www.getdowntonight.co.uk/?p=569</guid>
		<description><![CDATA[Just realised it has been a year since I posted anything on here! Time really flies when you&#8217;re busy! I remember when I was first starting off with jQuery, JSON and all that jazz that I struggled to find some simple examples of how to do things like send data to a web-service. Here is [...]]]></description>
			<content:encoded><![CDATA[<p>Just realised it has been a year since I posted anything on here! Time really flies when you&#8217;re busy!</p>
<p>I remember when I was first starting off with jQuery, JSON and all that jazz that I struggled to find some simple examples of how to do things like send data to a web-service. </p>
<p>Here is a quick example of how to send some JSON data (as a single post field) to a web service, using jQuery&#8217;s post method. </p>
<p>Keep in mind that if you are posting to another domain you may need to use something called jsonP &#8211; but if you are just learning, this is a good starting point!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;JSON Web Service Demo&lt;/title&gt;
&nbsp;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&nbsp;
        &lt;script src=&quot;http://code.jquery.com/jquery-1.7.1.min.js&quot;&gt;&lt;/script&gt;
&nbsp;
        <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
            $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">//this code will be added to the onclick event</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#save_user_button'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
                    <span style="color: #006600; font-style: italic;">//add the values we need for the API to an object</span>
                    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #006600; font-style: italic;">//build the object</span>
                    data.<span style="color: #660066;">guid</span>                   <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#guid'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    data.<span style="color: #660066;">defaultallocation</span>      <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#projecttype'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    data.<span style="color: #660066;">email</span>                  <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#email'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    data.<span style="color: #660066;">username</span>               <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#username'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    data.<span style="color: #660066;">fullname</span>               <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#fullname'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #006600; font-style: italic;">//turn the object in to a JSON string</span>
                    <span style="color: #003366; font-weight: bold;">var</span> myJson <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;http://testurl/service/user&quot;</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #006600; font-style: italic;">//call the post</span>
                    $.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>jsonUser <span style="color: #339933;">:</span> myJson<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>myResult<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>myResult<span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&nbsp;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;div&gt;
            &lt;table&gt;
&nbsp;
                &lt;tr&gt;
                    &lt;td&gt;Project Type&lt;/td&gt;
                    &lt;td&gt;&lt;input id=&quot;projecttype&quot;/&gt;&lt;/td&gt;
                &lt;/tr&gt;
&nbsp;
                &lt;tr&gt;
                    &lt;td&gt;GUID&lt;/td&gt;
                    &lt;td&gt;&lt;input id=&quot;guid&quot;/&gt;&lt;/td&gt;
                &lt;/tr&gt;
&nbsp;
                &lt;tr&gt;
                    &lt;td&gt;User Name&lt;/td&gt;
                    &lt;td&gt;&lt;input id=&quot;username&quot;/&gt;&lt;/td&gt;
                &lt;/tr&gt;
&nbsp;
                &lt;tr&gt;
                    &lt;td&gt;Full Name&lt;/td&gt;
                    &lt;td&gt;&lt;input id=&quot;fullname&quot;/&gt;&lt;/td&gt;
                &lt;/tr&gt;
&nbsp;
                &lt;tr&gt;
                    &lt;td&gt;Email&lt;/td&gt;
                    &lt;td&gt;&lt;input id=&quot;email&quot;/&gt;&lt;/td&gt;
                &lt;/tr&gt;
&nbsp;
            &lt;/table&gt;
&nbsp;
            &lt;input type=&quot;button&quot; id=&quot;save_user_button&quot; value=&quot;Save&quot;/&gt;
&nbsp;
        &lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.getdowntonight.co.uk/2011/12/sample-page-for-sending-json-data-as-string-to-web-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Loading JSON data &#8211; the EASY way!</title>
		<link>http://www.getdowntonight.co.uk/2009/11/loading-json-data-the-easy-way/</link>
		<comments>http://www.getdowntonight.co.uk/2009/11/loading-json-data-the-easy-way/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 12:22:04 +0000</pubDate>
		<dc:creator>Sam</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://www.getdowntonight.co.uk/?p=398</guid>
		<description><![CDATA[A few weeks ago I wrote a blog post about using jQuery and JSON to automatically load data from a JSON file in to name-matched controls on an HTML page. It is only now that I realise quite how powerful this is! The great thing about it is that you do not have to edit [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-403 colorbox-398" title="json160" src="http://www.getdowntonight.co.uk/wp-content/json160.gif" alt="json160" width="96" height="96" />A few weeks ago I wrote a blog post about using jQuery and JSON to automatically load data from a JSON file in to name-matched controls on an HTML page.</p>
<p><strong>It is only now that I realise quite how powerful this is!</strong></p>
<p>The great thing about it is that you do not have to edit your data-load function if you add a new field to the database. Because the jQuery function loops through all the fields in the JSON data, it will automatically insert the values in to the correct fields, providing those fields are named the same as the html form controls.</p>
<p>Here is the code again for those who dont want to check back.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/yourJson.js, function(myJson){
	$.each(myJson.rows, function(i,item) {
		 for (prop in item) {
		 	 $('#' + prop).val(item[prop]);
		 }
	});
});</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.getdowntonight.co.uk/2009/11/loading-json-data-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Inserting JSON data in to name-matched controls</title>
		<link>http://www.getdowntonight.co.uk/2009/09/inserting-json-data-in-to-name-matched-controls/</link>
		<comments>http://www.getdowntonight.co.uk/2009/09/inserting-json-data-in-to-name-matched-controls/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 12:08:10 +0000</pubDate>
		<dc:creator>Sam</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.getdowntonight.co.uk/?p=348</guid>
		<description><![CDATA[Thought this might be handy&#8230; This loop will insert the values from a JSON object in to text boxes. It loops around each key-pair and inserts the value if there is a text box with an ID that matches the key name. This is the JSON data: (yourJson.js) &#123;&#34;page&#34;:1,&#34;total&#34;:1,&#34;records&#34;:1,&#34;paging&#34;:20,&#34;rows&#34;:&#91; &#123;&#34;clipId&#34;:&#34;14792&#34;,&#34;barcode&#34;:&#34;02528&#34;,&#34;title&#34;:&#34;My Project Sample&#34;,&#34;format&#34;:&#34;DigiBeta&#34;,&#34;source&#34;:&#34;1242&#34;, &#34;runningTime&#34;:&#34;14 Mins&#34;,&#34;location&#34;:&#34;Library&#34;,&#34;timeCode&#34;:&#34;10:00:00&#34;,&#34;cUser&#34;:&#34;Sam&#34;, &#34;cDate&#34;:&#34;1 [...]]]></description>
			<content:encoded><![CDATA[<p>Thought this might be handy&#8230; This loop will insert the values from a JSON object in to text boxes.</p>
<p>It loops around each key-pair and inserts the value if there is a text box with an ID that matches the key name.</p>
<p>This is the JSON data: (yourJson.js)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;page&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;total&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;records&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;paging&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;rows&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;clipId&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;14792&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;barcode&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;02528&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;My Project Sample&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;format&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;DigiBeta&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;source&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;1242&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;runningTime&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;14 Mins&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;location&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Library&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;timeCode&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;10:00:00&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;cUser&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Sam&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;cDate&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;1 Jan 2007&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;mUser&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Sam&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;mDate&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;1 Jan 2008&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is the form HTML:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>form<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>table<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Clip Id<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'clipId'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Barcode<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'barcode'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Title<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Format<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'format'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Source<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'source'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Running Time<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'runningTime'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Location<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'location'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Time Code<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'timeCode'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></div></div>

<p>This is the jQuery:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/yourJson.js, function(myJson){
		$.each(myJson.rows, function(i,item) {
		    for (prop in item) {
				$('#' + prop).val(item[prop]);
		    }
		}); 
	});</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.getdowntonight.co.uk/2009/09/inserting-json-data-in-to-name-matched-controls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using XML in your jQuery to populate input boxes</title>
		<link>http://www.getdowntonight.co.uk/2009/08/using-xml-in-your-jquery-to-populate-input-boxes/</link>
		<comments>http://www.getdowntonight.co.uk/2009/08/using-xml-in-your-jquery-to-populate-input-boxes/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 18:06:13 +0000</pubDate>
		<dc:creator>Sam</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Science and Tech]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.getdowntonight.co.uk/?p=311</guid>
		<description><![CDATA[In a previous post I wrote about creating XML pages with MySQL data, using PHP. In this article, I&#8217;ll explain in simple terms how to parse that XML data and insert some of its values in to your page using jQuery. I&#8217;m going to keep this very simple, as there seem to be very few [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://www.getdowntonight.co.uk/2009/07/creating-xml-in-php/">previous post</a> I wrote about creating XML pages with MySQL data, using PHP. In this article, I&#8217;ll explain in simple terms how to parse that XML data and insert some of its values in to your page using jQuery.</p>
<p>I&#8217;m going to keep this very simple, as there seem to be very few examples of this on the net. It took me a while to work out a neat way of doing this, and I&#8217;m sure someone can improve on things. If so &#8211; please let me know in the comments.</p>
<p>First, lets set up an example. A very simple example. </p>
<p>Imagine an invoice. 4 boxes at the top of a grid, with a &#8216;Save&#8217; button that will add the contents of those boxes to the list below. The fields are such as &#8216;Item code&#8217;, &#8216;Description&#8217;, &#8216;Quantity&#8217; and &#8216;Unit Price&#8217;. The user enters the values and clicks the &#8216;Save&#8217; button to add that row to the list and clear the boxes, ready for the next row to be added. For the purposes of what we are doing, we will also want a 5th box (which would usually be hidden) to store the &#8216;Invoice Detail Id&#8217;.</p>
<p>Something like this:</p>
<pre>
<div class="wp_syntax">
<table>
<tr>
<td>Code
<input id="code" size='5'></td>
<td>Description
<input id="description"></td>
<td>Quantity
<input id="quantity" size='5'></td>
<td>Price
<input id="unitPrice" size='5'></td>
<td>
<input type='button' value='Save' onClick="alert('Please dont click');"></td>
<td><small>Hidden ID</small>
<input id="invoiceDetailId" size='5'></td>
</tr>
</table>
</div>
</pre>
<p>Creating this application is all quite trivial for someone experienced in PHP. However, what would be nice is to be able to edit each row without having to re-load the page. Well we can do this quickly and easily using jQuery.</p>
<p>Before we start with the jQuery code, we need to have ready a PHP page that will return valid XML with the details of an invoice detail record. Please see my <a href="http://www.getdowntonight.co.uk/2009/07/creating-xml-in-php/">previous post</a> for some help on how to do this.</p>
<p>This is an example output from the getDetailRow.php file.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;invoice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;invoiceDetail<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;invoiceDetailId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/invoiceDetailId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;code<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CR282<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/code<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Software Support<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;quantity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/quantity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;unitPrice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>40<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/unitPrice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/invoiceDetail<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;invoice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Against each row, add a button labeled &#8216;Edit&#8217;, and attach some jQuery code that calls a function called editDetailRow(detailRowId) &#8211; as the grid is built, insert this code in to each button with the parameter of that row&#8217;s detail ID. </p>
<p>Assuming that there is only ever one result in the XML output, you could use the following code to insert the values in to your input boxes.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">function</span> editDetailRow<span style="color: #009900;">&#40;</span>detailId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//first we need to load the XML data for that detail row</span>
    <span style="color: #006600; font-style: italic;">//if the function is a success it will call the function called processDetail</span>
    $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
       type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span>
       url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;getDetailRow.php?detailId=&quot;</span> <span style="color: #339933;">+</span> detailId<span style="color: #339933;">,</span>
       dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;xml&quot;</span><span style="color: #339933;">,</span>
       success<span style="color: #339933;">:</span> processDetail
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> processDetail<span style="color: #009900;">&#40;</span>xml<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//this function gets the results from the xml file</span>
    <span style="color: #006600; font-style: italic;">//and inserts them in to the boxes</span>
    $<span style="color: #009900;">&#40;</span>xml<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;invoiceDetail&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>   <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#code&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;code&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#description&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#quantity&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;quantity&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#unitPrice&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;unitPrice&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#invoiceDetailId&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;invoiceDetailId&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Yes, it really is that simple. You will need to add a reset button, and make sure you check for a value in the invoiceDetailId when you save (to either call an UPDATE or an INSERT) but thats it.</p>
<p>Hopefully you will find this useful and can use this as the basis for much more complicated XML and jQuery hookup&#8217;s.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.getdowntonight.co.uk/2009/08/using-xml-in-your-jquery-to-populate-input-boxes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

