<?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>www.mattginsberg.net &#187; PHP Tutorials</title>
	<atom:link href="http://www.mattginsberg.net/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mattginsberg.net</link>
	<description></description>
	<lastBuildDate>Wed, 12 May 2010 18:23:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tutorial: Validate an email address with PHP.</title>
		<link>http://www.mattginsberg.net/validate-email-address-php/</link>
		<comments>http://www.mattginsberg.net/validate-email-address-php/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 20:00:06 +0000</pubDate>
		<dc:creator>Matt Ginsberg</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.mattginsberg.net/?p=636</guid>
		<description><![CDATA[To validate an email using PHP is very simple and extremely easier to incorporate into your already made PHP form. There are a lot of reasons for having your form validate the email address. The most obvious one would be that you gather the proper information and the user does not make any simple mistakes. You might also have a user registration script, an online newsletter or a simple contact form. If the user forgets to put in a @ sign then the script will catch it and throw back ...]]></description>
			<content:encoded><![CDATA[<p>To validate an email using PHP is very simple and extremely easier to incorporate into your already made PHP form. There are a lot of reasons for having your form validate the email address. The most obvious one would be that you gather the proper information and the user does not make any simple mistakes. You might also have a user registration script, an online newsletter or a simple contact form. If the user forgets to put in a @ sign then the script will catch it and throw back an error message.</p>
<p>One php page named verify.php will contain all the code. Let&#8217;s create a simple contact form.</p>
<p><span id="more-636"></span></p>
<p><strong>HTML CODE:</strong></p>
<blockquote>
<div><code>&lt;form action="verify.php" enctype="multipart/form-data" method="post"&gt;Name:</code></div>
<div><code>&lt;input name="name" type="text" /&gt;</code></div>
<div><code>Email:</code></div>
<p><code>&lt;input name="email" type="text" /&gt;</code></p>
<p>Comments:</p>
<p>&lt;textarea cols=&#8221;20&#8243; rows=&#8221;2&#8243; name=&#8221;comments&#8221;&gt;&lt;/textarea&gt;</p>
<p>&lt;input name=&#8221;process&#8221; type=&#8221;hidden&#8221; value=&#8221;1&#8243; /&gt;</p>
<p>&lt;input type=&#8221;submit&#8221; value=&#8221;Verify!&#8221; /&gt;</p>
<p>&lt;/form&gt;</p></blockquote>
<p>We have three fields that the user can fill out: name, email and comments.</p>
<p>We also have a <em>hidden field</em> called &#8220;<em><strong>process</strong></em>&#8221; with the value of <strong>1</strong>. This field will allow our script to check if the form has been submitted as you will see later.</p>
<p>Our form is done and now we have to code our PHP script. Just so we stick to the topic, I am going to ignore the name and comments field and keep my attention to the  email field. With a success, it will display &#8220;Yay! You entered a valid email address&#8221;.</p>
<p>PHP CODE:</p>
<blockquote>
<div><code>if($_POST['process']==1)</code></div>
<div><code>{</code></div>
<div><code>    // IF THE EMAIL FIELD WAS LEFT BLANK</code></div>
<p><code>    if(empty($_POST['email']))</code></p>
<p>    {</p>
<p>        echo &#8216;&lt;p style=&#8221;color:#C00&#8243;&gt;Please enter your email address.&lt;/p&gt;&#8217;;</p>
<p>    }</p>
<p>    // IF USER ENTERED SOMETHING INTO THE EMAIL FIELD</p>
<p>    else</p>
<p>    {</p>
<p>        // IF USER ENTERED AN INVALID EMAIL ADDRESS</p>
<p>        if(preg_match(&#8216;/.*@.*\..*/&#8217;, $_POST['email']) &gt; 0)</p>
<p>        {</p>
<p>            echo &#8216;Yay! You entered a valid email address!&#8217;;</p>
<p>        }</p>
<p>        // IF USER ENTERED A VALID EMAIL ADDRESS</p>
<p>        else</p>
<p>        {</p>
<p>            echo &#8216;&lt;p style=&#8221;color:#C00&#8243;&gt;Please enter a valid email address.&lt;/p&gt;&#8217;;</p>
<p>        }</p>
<p>    }</p>
<p>}</p></blockquote>
<p>if($_POST[‘process’]==1) checks if the form has been submitted.</p>
<p>If the form has been submitted, then it makes sure the user did not leave the email field blank by using the empty function.</p>
<p>The preg_match function performs the email address validation, it searches what the user entered as an email address and checks if it matches the regular expression. If preg_match returns a 0 it means there was no match. If it returns a 1 it means there is at least one match.</p>
<p>Of course, the code does not let you know if the email address actually exists. It only makes sure you entered an email address in a valid format which would be the email, then the @ character, then the domain name, then a period and then something else (email@domain.extension).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mattginsberg.net/validate-email-address-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial: Adding CC ME to your PHP email form.</title>
		<link>http://www.mattginsberg.net/cc-me-php-form/</link>
		<comments>http://www.mattginsberg.net/cc-me-php-form/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 03:36:59 +0000</pubDate>
		<dc:creator>Matt Ginsberg</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.mattginsberg.net/?p=574</guid>
		<description><![CDATA[This is a very simple and easy way to make a CC ME in your HTML form. A CC ME is a great tool to have on your website. When the person is filling out the form on your website, give them the ability to copy themselves on the email as well. This way they have your email within their inbox so all they have to do is click on reply instead of coming back to your website. 
Take a look at the the Creating a form in PHP and ...]]></description>
			<content:encoded><![CDATA[<p>This is a very simple and easy way to make a CC ME in your HTML form. A CC ME is a great tool to have on your website. When the person is filling out the form on your website, give them the ability to copy themselves on the email as well. This way they have your email within their inbox so all they have to do is click on reply instead of coming back to your website. <span id="more-574"></span></p>
<p>Take a look at the the <a href="http://www.mattginsberg.net/creating-php-form/">Creating a form in PHP and XHTML</a> post I did earlier. Add the following code at the very bottom right above the ?&gt;.</p>
<blockquote>
<div><code><br />
//ccme<br />
if(@$_POST['ccme']){ //the name of the cc me checkbox (cc me)</code></div>
<p>$user = &#8220;&#8221;;<br />
$user .= &#8220;Thank you for contacting me. I usually reply within 24 hours. &#8220;;<br />
$user .= &#8220;\n&#8221;;<br />
$user .= &#8220;You are receiving this email because you clicked on &#8216;cc me&#8217; within the form you submitted on myself, www.mattginsberg.net.&#8221;;<br />
$user .= &#8220;\n &#8220;;<br />
$user .= &#8220;Here are your comments:;<br />
$user .= &#8220;\n&#8221;;<br />
$user .= $comments;<br />
$user .= &#8220;\n&#8221;;<br />
$user .= &#8220;\n &#8220;;<br />
$user .= &#8220;Thank you &#8220;;<br />
$user .= &#8220;Matt&#8221;;<br />
$EmailTo = &#8216;matt@mattginsberg.net &#8216;; //from me<br />
$from=$_POST['from']; //the name of the email input field on your form<br />
$subject=&#8217;Thank you for submitting the form at mattginsberg.net &#8216;.$_POST['subject'];<br />
mail($from,$subject,$user, &#8220;from:&lt;$EmailTo&gt;&#8221;);<br />
} </p></blockquote>
<p>To make this CC ME come to life, add a checkbox in your website&#8217;s form and name it ccme.</p>
<p>Feel free to break this down and ask me any <a href="http://www.mattginsberg.net/contact">questions</a> if you have any.</p>
<p>Don&#8217;t forget to leave comment&#8217;s!!!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mattginsberg.net/cc-me-php-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial: Creating a form in PHP and XHTML.</title>
		<link>http://www.mattginsberg.net/creating-php-form/</link>
		<comments>http://www.mattginsberg.net/creating-php-form/#comments</comments>
		<pubDate>Sat, 22 Nov 2008 05:36:26 +0000</pubDate>
		<dc:creator>Matt Ginsberg</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.mattginsberg.net/?p=540</guid>
		<description><![CDATA[This is the first of many PHP email PHP scripts. As you read on you will find out what else I have in store for you.
About 6 months ago I had to create my own PHP form to use with a website I was working with. After completing it, I was shocked at how easy it is. In this tutorial I will show you the basic&#8217;s of the PHP script and what it all means. After that, I will show you how to code it in your website.
Let&#8217;s take a ...]]></description>
			<content:encoded><![CDATA[<p>This is the first of many PHP email PHP scripts. As you read on you will find out what else I have in store for you.</p>
<p>About 6 months ago I had to create my own PHP form to use with a website I was working with. After completing it, I was shocked at how easy it is. In this tutorial I will show you the basic&#8217;s of the PHP script and what it all means. After that, I will show you how to code it in your website.</p>
<p>Let&#8217;s take a look at a custom PHP script. After that, I will break it down for you so you get an idea what what you are looking at.<br />
<span id="more-540"></span></p>
<h2>PHP Code</h2>
<blockquote><p><code>&lt;?php<br />
$EmailTo = "matt@mattginsberg.net";<br />
$Subject = "Subject Here";<br />
$fname = Trim(stripslashes($_POST['fname']));<br />
$lname = Trim(stripslashes($_POST['lname']));<br />
$from = Trim(stripslashes($_POST['from'])); </code><br />
<code><br />
//validation<br />
$validationOK=true;<br />
if (Trim($fname)=="") $validationOK=false; // user first name<br />
if (Trim($lname)=="") $validationOK=false; // user last name<br />
if (Trim($from)=="") $validationOK=false; // user email<br />
if (!$validationOK) {<br />
print "&lt;meta http-equiv=\"refresh\" content=\"0;URL=error.htm\"&gt;";<br />
exit;<br />
}</code><br />
<code>// prepare email body text<br />
$Body = "";<br />
$Body .= "First Name: ";<br />
$Body .= $fname;<br />
$Body .= "\n";<br />
$Body .= "Last Name: ";<br />
$Body .= $lname;<br />
$Body .= "\n";<br />
$Body .= "Email: ";<br />
$Body .= $from;<br />
$Body .= "\n";<br />
$Body .= Comments: ";<br />
$Body .= $comments;<br />
$Body .= "\n";<br />
</code></p>
<div><code><br />
$success = mail($EmailTo, $Subject, $Body, "From: &lt;$from&gt;");</code></div>
<div><code>// redirect to success page<br />
if ($success){<br />
print "&lt;meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\"&gt;";<br />
}<br />
else{<br />
print "&lt;meta http-equiv=\"refresh\" content=\"0;URL=error.htm\"&gt;";<br />
}</code></div>
<div><code>?&gt;</code></div>
<div><code> </code></div>
<div><code> </code></div>
<div><code> </code></div>
<p><code> </p>
<p> </p>
<p></code></p></blockquote>
<p>Now, lets break it down so you can understand it better. We will start at the top and work our way down</p>
<h2>PHP Code</h2>
<blockquote><p><code><br />
&lt;?php<br />
$EmailTo = "matt@mattginsberg.net";<br />
$Subject = "Subject Here";<br />
$fname = Trim(stripslashes($_POST['fname']));<br />
$lname = Trim(stripslashes($_POST['lname']));<br />
$from = Trim(stripslashes($_POST['from']));<br />
</code></p></blockquote>
<p>This PHP code tells the form where it is going. First thing is first, the command &lt;?php always starts your PHP script. Now, if you notice the EmailTo, that is your email address. The Subject is listed there as well, I would assume you understand what that means. You would want to type out the actual subject. <strong>Example, if you are putting this php/html form in your quote section of the website, then you would type in &#8211; Quote. </strong>However, lets take a look at $fname, $lname, and $from. The $fname (firstname) is the name of the text feild in your HTML form. The name would be fname, not $fname. Same thing for lname and from. $from is the end users (or person submitting the form) email. Lets move on.</p>
<h2>PHP Code</h2>
<blockquote><p><code><br />
$validationOK=true;<br />
if (Trim($fname)=="") $validationOK=false; // user first name<br />
if (Trim($lname)=="") $validationOK=false; // user last name<br />
if (Trim($from)=="") $validationOK=false; // user email<br />
if (!$validationOK) {<br />
print "&lt;meta http-equiv=\"refresh\" content=\"0;URL=error.htm\"&gt;";<br />
exit;<br />
}</code></p></blockquote>
<p>This PHP reviews the items that are required to be filled out by the end users. If you do not reguire this, then do not put it in. You will notice a if () command. If the end user doesn&#8217;t fill out a text feild, check box, bullet, they will be directed to an error page that you specify. You must create one!! Lets move on.</p>
<h2>PHP Code</h2>
<blockquote>
<div><code>// prepare email body text<br />
$Body = "";<br />
$Body .= "First Name: ";<br />
$Body .= $fname;<br />
$Body .= "\n";<br />
$Body .= "Last Name: ";<br />
$Body .= $lname;<br />
$Body .= "\n";<br />
$Body .= "Email: ";<br />
$Body .= $from;<br />
$Body .= "\n";<br />
$Body .= Comments: ";<br />
$Body .= $comments;<br />
$Body .= "\n";<br />
$success = mail($EmailTo, $Subject, $Body, "From: &lt;$from&gt;"); </code></div>
</blockquote>
<p>This PHP code is the body of the email you receive when the end user submits their form. The command $body will <strong>NOT</strong> be listed in your HTML form. The $success command will look at the validation and then will send the email to you with the subject you provided along with the body (first name, last name, email and comments). Lets move on.</p>
<h2>PHP Code</h2>
<blockquote>
<div><code><br />
// redirect to success page<br />
if ($success){<br />
print "&lt;meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\"&gt;";<br />
}<br />
else{<br />
print "&lt;meta http-equiv=\"refresh\" content=\"0;URL=error.htm\"&gt;";<br />
}<br />
?&gt;<br />
</code></div>
</blockquote>
<p>This PHP code is mainly for the $success. If the validation is correct, your form will take you to the ok.htm file (that you create and put in the same directory at this php file). If the $success failed, it moves to the else {} command, which tells the php form to send it to the error.htm page that you also created. The error.htm page within the $success can be different than the error.htm page within the validation section of this code. Specify one as error1.htm and error2.htm. Also, the command ?&gt; is used to close the PHP script.</p>
<p>Lets move on to the HTML Code.</p>
<h2>HTML Code</h2>
<blockquote><p><code>&lt;p&gt;&lt;font color="#EE0000" size="2" face="Verdana, Arial, Helvetica, sans-serif"&gt;*&lt;/font&gt;&lt;font size="2" face="Verdana, Arial, Helvetica, sans-serif"&gt;required&lt;/font&gt;&lt;/p&gt;<br />
&lt;p&gt;&lt;font color="#EE0000" size="2" face="Verdana, Arial, Helvetica, sans-serif"&gt;*&lt;/font&gt;&lt;font face="Verdana, Arial, Helvetica, sans-serif"&gt;First Name:&lt;/font&gt;<br />
&lt;input name="fname" type="text" id="fname" size="20" style="margin-left:50px"&gt;<br />
&lt;br&gt;<br />
&lt;br&gt;<br />
&lt;font color="#EE0000" size="2" face="Verdana, Arial, Helvetica, sans-serif"&gt;*&lt;/font&gt;&lt;font size="2" face="Verdana, Arial, Helvetica, sans-serif"&gt;&lt;/font&gt;&lt;font face="Verdana, Arial, Helvetica, sans-serif"&gt;Last Name: &lt;/font&gt;<br />
&lt;input name="lname" type="text" id="lname" size="20" style="margin-left:49px"&gt;<br />
&lt;br&gt;<br />
&lt;br&gt;<br />
&lt;font color="#EE0000" size="2" face="Verdana, Arial, Helvetica, sans-serif"&gt;*&lt;/font&gt;&lt;font size="2" face="Verdana, Arial, Helvetica, sans-serif"&gt;&lt;/font&gt;&lt;font face="Verdana, Arial, Helvetica, sans-serif"&gt;Email: &lt;/font&gt;<br />
&lt;input name="from" type="text" id="from" size="20" style="margin-left:92px"&gt;<br />
&lt;/p&gt;<br />
&lt;p&gt;&lt;font face="Verdana, Arial, Helvetica, sans-serif"&gt;Comments:&lt;/font&gt;<br />
&lt;textarea name="comments" id="comments" cols="25" rows="5" style="margin-left:62px"&gt;&lt;/textarea&gt;<br />
&lt;/p&gt;<br />
&lt;p&gt;&amp;nbsp;&lt;/p&gt;<br />
&lt;p&gt;<br />
&lt;input type="submit" name="button" id="button" value="Submit"&gt;<br />
&lt;/p&gt;<br />
</code></p></blockquote>
<p><strong>Hint: Make you you enter the proper form tags before and after the above XHTML code. If you are unaware of what they are, <a href="http://www.mattginsberg.net/contact">please let me know</a>.</strong></p>
<p>This basic HTML will get the code job for you. Just copy and paste the PHP and HTML into your web site editing software and have fun.</p>
<p>Stay tuned, I am going to create more PHP tutorials. These will be considered addons. They will include, a &#8216;cc me&#8217; checkbox, email validation, and a captcha. The cc me will be done with PHP, the other will be done with JavaScript.</p>
<p>The cc me is when the end user wants a copy of the form they submitted to you. The email validation will not allow people to type in asdfasf@adsfasdf.com. It will make sure it is a proper email. The captcha will put a simple picture with numbers that the end user needs to fill in. In doing so, they will submit the form. Those to come sooner than later. Why dont you <a href="http://feeds.feedburner.com/mattginsbergdotnet" target="_blank">subscribe</a> to my blog?</p>
<p>If you have any questions at all, please <a href="http://www.mattginsberg.net/contact">contact</a> me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mattginsberg.net/creating-php-form/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
