<?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>Cultivate Web Design Melbourne - Development Blog &#187; code</title>
	<atom:link href="http://cultivatewebdesign.com.au/blog/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://cultivatewebdesign.com.au/blog</link>
	<description></description>
	<lastBuildDate>Thu, 12 Aug 2010 04:16:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using JASON to retrive data from php and populate form fields</title>
		<link>http://cultivatewebdesign.com.au/blog/using-jason-to-retrive-data-from-php-and-populate-form-fields/</link>
		<comments>http://cultivatewebdesign.com.au/blog/using-jason-to-retrive-data-from-php-and-populate-form-fields/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 03:08:00 +0000</pubDate>
		<dc:creator>Chris Gunn</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[code example]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[JASON]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://cultivatewebdesign.com.au/blog/?p=96</guid>
		<description><![CDATA[Say we want to auto fill the rest of the fields when some one enters the member id, &#60;form&#62; Member id:&#60;input id=&#34;memberid&#34; /&#62;&#60;br /&#62; First name:&#60;input id=&#34;firstname&#34; /&#62;&#60;br /&#62; Last name:&#60;input id=&#34;lastname&#34; /&#62;&#60;br /&#62; Phone:&#60;input id=&#34;phone&#34; /&#62;&#60;br /&#62; Email:&#60;input id=&#34;email&#34; /&#62;&#60;br /&#62; &#60;input type=&#34;submit&#34; /&#62; &#60;/form&#62; Now we make a php file to simply return [...]]]></description>
			<content:encoded><![CDATA[<p>Say we want to auto fill the rest of the fields when some one enters the member id,</p>
<pre class="brush: xml;">
&lt;form&gt;
	Member id:&lt;input id=&quot;memberid&quot; /&gt;&lt;br /&gt;
	First name:&lt;input id=&quot;firstname&quot; /&gt;&lt;br /&gt;
	Last name:&lt;input id=&quot;lastname&quot; /&gt;&lt;br /&gt;
	Phone:&lt;input id=&quot;phone&quot; /&gt;&lt;br /&gt;
	Email:&lt;input id=&quot;email&quot; /&gt;&lt;br /&gt;
	&lt;input type=&quot;submit&quot; /&gt;
&lt;/form&gt;
</pre>
<p>Now we make a php file to simply return the memeber info from for a POSTed id.<br />
The use of JASON here will let us do a neat trick when populating the fields using Javascript.<br />
If the memebrs not found, setting the header to &#8217;500&#8242; will return fail for the jason request.</p>
<pre class="brush: php;">
//load_member.php
$id = mysql_escape_string($_POST['id']);
$array = mysql_fetch_assoc(mysql_query(&quot;SELECT firstname, lastname, phone, email FROM members WHERE id = '$id' &quot;));
if(is_array($array))
	echo json_encode($array);
else
	header('HTTP/1.1 500 Internal Server Error');
</pre>
<p>Now, when a user moves the curser out of the memeber id input box, we will call the php file above though a normal javascript POST request.</p>
<pre class="brush: jscript;">
$('memberid').addEvent('blur',function()
{	new Request.JSON(
	{	url: &quot;load_member.php&quot;,
		onFailure:function()
		{	alert(&quot;Sorry, we couldn't find the id.&quot;);
		},
		onSuccess: function(member)
		{	for (var member_details in member)
			{	$(member_details).set('value',member[member_details]);
			}
		}
	}).post({'id': this.get('value')});
});
</pre>
<p>If we named the form id&#8217;s the same as table fields or used</p>
<pre class="brush: sql;"> SELECT field_name AS html_id_name FROM table</pre>
<p>then we can use the &#8216;for loop&#8217; to populate the fields.</p>
]]></content:encoded>
			<wfw:commentRss>http://cultivatewebdesign.com.au/blog/using-jason-to-retrive-data-from-php-and-populate-form-fields/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

