<?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; php</title>
	<atom:link href="http://cultivatewebdesign.com.au/blog/tag/php/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>Easy forms building with regular expressions</title>
		<link>http://cultivatewebdesign.com.au/blog/easy-forms-building-with-regular-expressions/</link>
		<comments>http://cultivatewebdesign.com.au/blog/easy-forms-building-with-regular-expressions/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 13:00:47 +0000</pubDate>
		<dc:creator>Chris Gunn</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[save form values]]></category>

		<guid isPermaLink="false">http://cultivatewebdesign.com.au/blog/?p=100</guid>
		<description><![CDATA[Hate if or love it, forms are a daily part of a developers life. In this post I will show you a few ways to ease the pain. Say we have this, &#60;form method=&#34;post&#34;&#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; [...]]]></description>
			<content:encoded><![CDATA[<p>Hate if or love it, forms are a daily part of a developers life. In this post I will show you a few ways to ease the pain.</p>
<p>Say we have this,</p>
<pre class="brush: xml;">
&lt;form method=&quot;post&quot;&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>And we want to end up like this one below:</p>
<pre class="brush: xml;">
&lt;form method=&quot;post&quot;&gt;
	Member id:&lt;input name=&quot;memberid&quot; value=&quot;&lt;?=$_POST['memberid']?&gt;&quot; id=&quot;memberid&quot; /&gt;&lt;br /&gt;
	First name:&lt;input name=&quot;firstname&quot; value=&quot;&lt;?=$_POST['firstname']?&gt;&quot; id=&quot;firstname&quot; /&gt;&lt;br /&gt;
	Last name:&lt;input name=&quot;lastname&quot; value=&quot;&lt;?=$_POST['lastname']?&gt;&quot; id=&quot;lastname&quot; /&gt;&lt;br /&gt;
	Phone:&lt;input name=&quot;phone&quot; value=&quot;&lt;?=$_POST['phone']?&gt;&quot; id=&quot;phone&quot; /&gt;&lt;br /&gt;
	Email:&lt;input name=&quot;email&quot; value=&quot;&lt;?=$_POST['email']?&gt;&quot; id=&quot;email&quot; /&gt;&lt;br /&gt;
	&lt;input type=&quot;submit&quot; /&gt;
&lt;/form&gt;
</pre>
<p>So what do we do? We ask a little help from our mate, regEx.<br />
We search for this</p>
<pre class="brush: plain;"> id=&quot;([a-z0-9_-]+)&quot; </pre>
<p>and replace with this</p>
<pre class="brush: plain;"> name=&quot;$1&quot; id=&quot;$1&quot; </pre>
<p>. Now for values search for</p>
<pre class="brush: plain;"> name=&quot;([a-z0-9_-]+)&quot; </pre>
<p>and replace with</p>
<pre class="brush: plain;"> name=&quot;$1&quot; value=&quot;&lt;?=$_POST['$1']?&gt;&quot; </pre>
<p>and bang! It&#8217;s done!</p>
<p>Now a little tip on how to convert those nasty</p>
<pre class="brush: xml;">
&lt;select name=&quot;country&quot;&gt;
	&lt;option value=&quot;Australia&quot;&gt;Australia&lt;/option&gt;
	&lt;option value=&quot;Austria&quot;&gt;Austria&lt;/option&gt;
	&lt;option value=&quot;Bahamas&quot;&gt;Bahamas&lt;/option&gt;
	....
&lt;/select&gt;
</pre>
<p>to</p>
<pre class="brush: xml;">
&lt;select name=&quot;country&quot;&gt;
	&lt;option value=&quot;Australia&quot; &lt;?=$_POST['country']=='Australia'?' selected ':''?&gt;&gt;Australia&lt;/option&gt;
	&lt;option value=&quot;Austria&quot; &lt;?=$_POST['country']=='Austria'?' selected ':''?&gt;&gt;Austria&lt;/option&gt;
	&lt;option value=&quot;Bahamas&quot; &lt;?=$_POST['country']=='Bahamas'?' selected ':''?&gt;&gt;Bahamas&lt;/option&gt;
	....
&lt;/select&gt;
</pre>
<p>All you have to do is to replace</p>
<pre class="brush: plain;">&lt;option value=&quot;([A-Za-z0-9_ -]+)&quot;&gt;([A-Za-z0-9_ -]+)&lt;/option&gt;</pre>
<p>with</p>
<pre class="brush: plain;">&lt;option value=&quot;$1&quot; &lt;?=$_POST['country']=='$1'?' selected ':''?&gt;&gt;$2&lt;/option&gt;</pre>
<p>Now only if designing forms with out tables was this easy <img src='http://cultivatewebdesign.com.au/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cultivatewebdesign.com.au/blog/easy-forms-building-with-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>1</slash:comments>
		</item>
	</channel>
</rss>

