<?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>Sean Monahan &#187; Object-Oriented</title>
	<atom:link href="http://seanmonahan.org/tag/object-oriented/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanmonahan.org</link>
	<description></description>
	<lastBuildDate>Wed, 07 Dec 2011 04:20:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Create HTML Forms with Object-Oriented PHP</title>
		<link>http://seanmonahan.org/2008/12/18/create-html-forms-with-object-oriented-php/</link>
		<comments>http://seanmonahan.org/2008/12/18/create-html-forms-with-object-oriented-php/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 23:23:04 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[HTML Forms]]></category>
		<category><![CDATA[Object-Oriented]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP5]]></category>

		<guid isPermaLink="false">http://seanmonahan.org/?p=122</guid>
		<description><![CDATA[One of the things I really dislike about PHP is it&#8217;s reliance on spaghetti code.  Every PHP developer has had to deal with &#60;?php ?&#62; tags strewn about HTML in a seeming mess.  The worst situation is updating someone else&#8217;s code.  Often it takes longer to decipher everything than it does to make the actual [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things I really dislike about PHP is it&#8217;s reliance on spaghetti code.  Every PHP developer has had to deal with &lt;?php ?&gt; tags strewn about HTML in a seeming mess.  The worst situation is updating someone else&#8217;s code.  Often it takes longer to decipher everything than it does to make the actual update.<br />
<span id="more-122"></span><br />
I&#8217;ve been working on a project that takes a lot of user input from HTML forms and I decided at the beginning that I was not going to deal with a bunch of spaghetti code to get things done.  Rather I wrote a serious of form classes in PHP5.  These classes allow me to write HTML forms without having to write a single line of HTML &#8212; everything is handled in PHP and it has definitely made life easier.</p>
<p>Now I&#8217;m going to share the love so no one else has to suffer through a mess of convoluted code.</p>
<h3>The Classes</h3>
<p>My form class libary has seven classes.  Each distinct form element type (e.g., &lt;input&gt;, &lt;select&gt;, etc.) is represented by its own class.  All these elements are then pulled together in the Form class that handles the actual construction and ouputting of the form to the screen.</p>
<p>Download the Form class library <a href="http://seanmonahan.org/wp-content/uploads/2008/12/formclasslibrary.zip">here</a>.</p>
<p>See the complete documentation for the Form class library <a href="http://seanmonahan.org/code/php/docs">here</a>.</p>
<p>Download the examples <a href="http://seanmonahan.org/wp-content/uploads/2008/12/formexamples.zip">here</a>.</p>
<h3>The Examples</h3>
<p>So, you&#8217;re thinking, &#8220;this is cool, but can I see it in action?&#8221; Just read on.</p>
<h4>A Simple Form</h4>
<p>The first example is a simple form with one text input and a submit button.</p>
<pre class="brush: php; title: ; notranslate">

// Create a new Form object

$form = new Form(&quot;ExampleForm&quot;);

// Add a text input

$form-&gt;addField(new FormInput(&quot;&quot;, &quot;text&quot;, &quot;firstname&quot;, &quot;What is your name?&quot;));

// Add a submit button

$form-&gt;addField(new FormInput(&quot;Submit&quot;, &quot;submit&quot;, &quot;submit&quot;));

// Print the form to the screen

$form-&gt;printForm();
</pre>
<p><a href="http://seanmonahan.org/code/php/examples/form/SimpleForm.php" target="_blank">See the example</a> in action.</p>
<h4>A Simple Form with a Select</h4>
<p>The second example adds a form select (drop down) menu, sets the default item in the select menu and shows off a different way to print forms.</p>
<pre class="brush: php; title: ; notranslate">

// Create a new Form object
$form = new Form(&quot;ExampleForm&quot;);

// Add a text input
$form-&gt;addField(new FormInput(&quot;&quot;, &quot;text&quot;, &quot;firstname&quot;, &quot;What is your name?&quot;));

// Create some data for our select.
$genders = array();

$genders[] = array(&quot;value&quot; =&gt; &quot;M&quot;, // The value your script will see when the form is submitted.
&quot;name&quot; =&gt; &quot;Man&quot;, // The name that will be displayed to users.
&quot;selected&quot; =&gt; false);

$genders[] = array(&quot;value&quot; =&gt; &quot;W&quot;,
&quot;name&quot; =&gt; &quot;Woman&quot;,
&quot;selected&quot; =&gt; true); // Set this item to be selected by default

// Create a select.
$genderSelect = new FormSelect($genders, &quot;gender&quot;, &quot;Your are a: &quot;);

// Add our select
$form-&gt;addField($genderSelect);

// Add the submit button.
$form-&gt;addField(new FormInput(&quot;Submit&quot;, &quot;submit&quot;, &quot;submit&quot;));

// Save the form as an HTML string.
$html = $form-&gt;buildForm();

// Print the form later.
echo $html;
</pre>
<p><a href="http://seanmonahan.org/code/php/examples/form/SimpleFormWithSelect.php" target="_blank">See the example</a> in action.</p>
<h4>A Less Simple Form with TextArea</h4>
<p>This next example adds a textarea and provides some default text for it as well as the number of columns and rows.</p>
<pre class="brush: php; title: ; notranslate">

// Create the text area.
$defaultText = &quot;Tell us about yourself.  In 500 words or less.&quot;;
$textarea = new FormTextArea($defaultText, &quot;biography&quot;);

// You can also set properties like this.
$textarea-&gt;setColumns(20);
$textarea-&gt;setRows(5);

// Add the text area.
$form-&gt;addField($textarea);
</pre>
<p><a href="http://seanmonahan.org/code/php/examples/form/SimpleFormWithTextarea.php" target="_blank">See the example</a> in action.</p>
<h4>Organize Things with Separators</h4>
<p>Our example form is starting to get big.  Time to bring some organization into the mix.  Enter separators.  Separtors are purely organizational elements and will not be submitted along with your form.  They simple allow us to visually break up the form so it&#8217;s easier to use.</p>
<pre class="brush: php; title: ; notranslate">

// Add a separator with the text &quot;Bio&quot;

$form-&gt;addField(new FormSeparator(&quot;Bio&quot;)); // Want the separator without any text?  Just take out &quot;Bio&quot; and you've got it.
</pre>
<p><a href="http://seanmonahan.org/code/php/examples/form/SimpleFormWithSeparators.php" target="_blank">See the example</a> in action.</p>
<h4>Block Bots with CAPTCHA</h4>
<p>The final example adds CAPTCHA to our form.  This class uses Securimage CAPTCHA and you&#8217;ll need to download it from <a href="http://www.phpcaptcha.org/" target="_blank">here</a> if you want to use the CAPTCHA features of this class library.  The FormCaptcha object is a little more complicated than the rest so it&#8217;s easier to create a new object and set the properties afterward.</p>
<pre class="brush: php; title: ; notranslate">

// Create a new FormCaptcha object.
// NOTE: the source you pass to the contructor is the URI.  In other words,
// you could slap http://www.seanmonahan.org on front of the source and find that script.
$captcha = new FormCaptcha(&quot;/code/php/examples/form/Captcha/securimage_show.php&quot;);

// Position the reload text to be placed after the image.
$captcha-&gt;setReloadLocation(&quot;AfterImage&quot;);

// Set the label for the CAPTCHA input field.
$captcha-&gt;setLabel(&quot;Prove you aren't a bot.&quot;);

// Add the CAPTCHA to the form.
$form-&gt;addField($captcha);
</pre>
<p><a href="http://seanmonahan.org/code/php/examples/form/SimpleFormWithCaptcha.php" target="_blank">See the example</a> in action.</p>
]]></content:encoded>
			<wfw:commentRss>http://seanmonahan.org/2008/12/18/create-html-forms-with-object-oriented-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

