<?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>code-diesel &#187; html</title>
	<atom:link href="http://www.codediesel.com/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Validating POST fields the easy way</title>
		<link>http://www.codediesel.com/php/validating-post-fields-the-easy-way/</link>
		<comments>http://www.codediesel.com/php/validating-post-fields-the-easy-way/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 15:47:44 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[html]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=142</guid>
		<description><![CDATA[Validating POST data from a form is a common requirement for a developer. If the number of form fields are few than the validation is a small matter. But the case is different when the form contains more than 15 or 20 fields and some of the fields are mandatory. The following code will give you an idea [...]]]></description>
			<content:encoded><![CDATA[<p>Validating POST data from a form is a common requirement for a developer. If the number of form fields are few than the validation is a small matter. But the case is different when the form contains more than 15 or 20 fields and some of the fields are mandatory. The following code will give you an idea of how to easily validate mandatory fields, whatever the number of fields.<br />
<span id="more-142"></span><br />
The first step is to prefix a &#8216;c_&#8217; or any other character of your choice to a form field that is mandatory. For example I&#8217;ve prefixed a &#8216;c_&#8217; for the email field below.</p>

<div class="wp_codebox"><table><tr id="p1423"><td class="code" id="p142code3"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>input name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;c_email&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;c_email&quot;</span> maxlength<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;40&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> <span style="color: #339933;">/&gt;</span></pre></td></tr></table></div>

<p>Once we have added the required prefix we can check for them using the following code.</p>

<div class="wp_codebox"><table><tr id="p1424"><td class="code" id="p142code4"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* Initialize $_SESSION variables to hold errors and form variables */</span>
<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'formVars'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'errors'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Copy all $_POST variables to $_SESSION['formVars'] */</span>
<span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$varname</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * For form elements that are compulsory check 
     * to see if they are empty. To ease this ,form 
     * elements that are compulsory have a 'c_' prefix added,
     * we can use that to parse the compulsory fields.
     **/</span>
&nbsp;
     <span style="color: #009933; font-style: italic;">/* Get the field prefix */</span>
    <span style="color: #000088;">$prefix</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$varname</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'formVars'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$varname</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$prefix</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;c_&quot;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'errors'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$varname</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$varname</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$varname</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; field cannot be empty&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now it doesn&#8217;t matter how many form fields you have, the code remains the same. You can extend the idea for other validation purpose. For example you can add a double prefix like &#8216;ci_&#8221; to indicate that the field is mandatory with a integer datatype and make the corresponding change in the validation code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/validating-post-fields-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

