Saturday, 28 January 2012

How To Code A PHP Contact Form By Hand

I'm more of a designer than a developer, using found code snippets, plugins and programs in JavaScript and PHP to get stuff done on the websites I make. Thankfully, there's a lot of them about: Dynamic Drive, the Yahoo! Developer Network, and any number of handy applications programs on SourceForge. Add to that the friendly folks who graciously post useful code in the blogs I follow (thank you, Smashing Magazine!), and it's no wonder I spend so much time on Twitter looking out for more maps to gold mines links to useful blog posts.

Unfortunately, there are times when my usual sources of information don't meet my needs, so I end up having to go on help forums (thankfully, there are loads of them) to get the information I need to solve a particular problem. The result is that I end up with the tools I need to pay it forward where handy code snippets are concerned, as well as sorting out my own problems.

The problem

I can make contact email webforms easily enough using the code provided by W3 Schools. Not only do they work perfectly, they have a feature for keeping spammers from messing with the headers. The trouble is, W3 Schools have provided information for processing forms, but not for linking elements such as radio buttons and check boxes to them in a way I find easy to understand.

They tell you how to make the radio buttons and check boxes, but not how to integrate them into a mailer.php so you can see which options were chosen by the respondent.

I went on other tutorial websites and encountered the same problem: the instructions were either inadequate or hard to understand, so I didn't know how to apply them. Finally, I gave up and went with JotForm just so I could have a working contact form with working radio buttons and check boxes until I found the code I needed.

The solution

I discovered PHP Builder a few days ago, when I discovered JotForm, but went with JotForm because there wasn't a solution to my problem already on the forum. It took a while to get there, but after some considerable backing and forthing, we arrived there at the end. My Social Media mailer works as intended, recording the choices made by respondents and passing them to me in neat paragraph blocks.

To make this work, you need to make two pages: social_m_prices.html and social_m_price-mailer.php, or whatever you want to call them.

The markup:

<form method="post" action="social_m_prices-mailer.php"> <div>Name:<span class="ss-required-asterisk">*</span><br> <input name="name" size="45" type="text"><br> <br>

Email:<span class="ss-required-asterisk">*</span><br>

<input name="email" size="45" type="text"><br>  <br>  <input name="Facebook" value="Facebook" type="checkbox"> Facebook &nbsp;  <input name="Linked_In" value="Linked_In" type="checkbox"> Linked In &nbsp;  <input name="Twitter" value="Twitter" type="checkbox"> 

Twitter &nbsp;

  <input name="Qype" value="Qype" type="checkbox"> Qype &nbsp;  <input name="Meengle" value="Meengle" type="checkbox"> Meengle<br><br>	    
<div style="text-align: center;"><big style="font-weight: bold;"><br> &pound;35 each or &pound;150 for all five.</big><br> </div> <br>

Would you like to have a BT Tradespace account?<br>  <br>  <input name="BT-Tradespace" value="Bronze" type="radio">Bronze  <input name="BT-Tradespace" value="Silver" type="radio">Silver  

<input name="BT-Tradespace" value="Gold" type="radio">Gold</div> <br>

<br> <br>Comments:<span class="ss-required-asterisk">*</span><br>

  <br>  <textarea rows="14" name="message" cols="45"> </textarea> <br>  <br>  <input value="Submit" name="submit" type="submit"></form>

The code:


function spamcheck($field) {

//filter_var() sanitizes the e-mail


$field=filter_var($field, FILTER_SANITIZE_EMAIL);

//filter_var() validates the e-mail


if(filter_var($field, FILTER_VALIDATE_EMAIL)) {

return TRUE;

} else {

return FALSE;



$social_media = '';

if (isset($_POST['Facebook']) &&$_POST['Facebook'] ==
'Facebook') {

$social_media .= "Facebook: checked\\n";

} else {

$social_media .= "Facebook: unchecked\\n";


if (isset
($_POST['Linked_In']) && $_POST['Linked_In'] == 'Linked_In') {

$social_media .= "Linked In: checked\\n";

} else {

$social_media .= "Linked In: unchecked\\n";


if (isset($_POST['Twitter']) && $_POST['Twitter'] == 'Twitter')

$social_media .= "Twitter: checked\\n";

} else {

$social_media .= "Twitter: unchecked\\n";


if (isset($_POST
['Qype']) && $_POST['Qype'] == 'Qype') {

$social_media .= "Qype: checked\\n";

} else {

$social_media .= "Qype: unchecked\\n";


if (isset($_POST['Meengle']) && $_POST['Meengle'] == 'Meengle')

$social_media .= "Meengle: checked\\n";

} else {

$social_media .= "Meengle: unchecked\\n";


$bt_tradespace = 'BT-Tradespace: ';

if (isset($_POST['BT-Tradespace'])) {

if ($_POST['BT-Tradespace'] == 'Bronze') {

$bt_tradespace .= "Bronze\\n";

} else if ($_POST['BT-Tradespace'] == 'Silver') {

$bt_tradespace .= "Silver\\n";

} else if ($_POST['BT-Tradespace'] == 'Gold') {

$bt_tradespace .= "Gold\\n";


} else {

$bt_tradespace .= "Not Answered\\n";


if (isset($_REQUEST
['email'])) {

//if "email" is filled out, proceed

//check if the email address is invalid

$mailcheck = spamcheck

if ($mailcheck==FALSE) {

echo "Invalid input";

} else {

//send email

$email = $_REQUEST['email'] ;

$subject = $_REQUEST['subject'] ;

$message = '. "\\n\\n" .

Name: '. $_REQUEST['name'] .'

Email: '. $_REQUEST['email'] .'

Message: '. $_REQUEST['message'] .'

Social Media:

'. $social_media . "\\n\\n" .'

'. $bt_tradespace . "\\n\\n" .'

Contact received on: '. date('m/d/y h:i a');

$to = "";

$header = "From:" . PHP_EOL;

$header .= "Reply to: $email" . PHP_EOL;

if( mail($to, "Subject: $subject", $message, $header ) ) {

echo "Message sent successfully. ";

echo "Thanks. I'll get back to you within twenty four hours.";

} else {

echo "Oh dear, there seems to have been a mistake. Please try again.";



} else {

//if "email" is not filled out, display the form

echo "<form method='post' action='social_m_prices-mailer.php'>

Email: <input name='email' type='text' /><br />

Subject: <input name='subject' type='text' /><br />

Message:<br />

<textarea name='message' rows='15' cols='40'>

</textarea><br />

<input type='submit' />





The above code produces results that come up like this in the email notifications I receive:

Name: Wendy
Message: test

Social Media:
Facebook: checked
Linked In: checked
Twitter: checked
Qype: unchecked
Meengle: unchecked

BT-Tradespace: Silver

Contact received on: 07/05/11 06:42 am

Which is just what I wanted: notification of which choices were made and spaces between the sections to make them easier to read. The order form is linked to the Home page via a call-to-action button.

Now I've got someone asking how to link up the results from a drop-down list, so I'd better get cracking on that.

Update: while trying to find a way to display PHP code without it executing on the page, I found this useful online tool: Simple Code. I really am constantly amazed at the generosity of the people who make these things available for free.

No comments:

Post a Comment