Forum Moderators: coopster

Message Too Old, No Replies

Contact php form script

simple php form

         

apprentice

8:08 pm on Mar 28, 2008 (gmt 0)

10+ Year Member



Dear all,

I need a PHP contact form script that I can use on a website. I searched around for a while and came across a few freebies, pretty close to my needs (phpformmail, a Free PHP Contact Us Form - found at ibdhost etc). What I want is a basic form in terms of functionality, no more than couple pages long (code) that will include Name, Email address and a Comment area - just those 3 elements. I also want the form to provide some basic protection against spam bots i.e. doesn't reveal the email address, no obvious loopholes and so on. At the moment I am not looking for a captcha enabled form – first I 'd like to get a basic form working.

I need your valuable opinion/recommendation as to which form should I look based on the above?

Regards

jatar_k

9:05 pm on Mar 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What was lacking in the one you found? I would start with that and then go from there.

You could also look at this thread from our library. It might help you understand what is going on.

Basics of Submitting and Emailing Forms with PHP [webmasterworld.com]

apprentice

10:47 pm on Mar 28, 2008 (gmt 0)

10+ Year Member



I didn't mean to create the assumption that there was something wrong with those forms. It's just that there is always something better; it's just a case of finding it - and many people on this forum would certainly be able to point me towards the right direction - which is what you did. That link was most helpful in understanding the basics, as my PHP knowledge is not my forte!

I was hoping to understand how everything works so that, maybe later on, I can implement Kcaptcha to it.

Regards

jatar_k

1:19 pm on Mar 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



those one syou found are just fine as well, though they often need a bunch of trimming so that things you aren't using aren't there. Changing the code to make it specific to you helps protect from automated attacks based on those scripts. It also helps you understand what's happening on your own site.

Dave75

7:32 pm on Mar 29, 2008 (gmt 0)

10+ Year Member



What I want is a basic form in terms of functionality, no more than couple pages long (code) that will include Name, Email address and a Comment area - just those 3 elements. I also want the form to provide some basic protection against spam bots i.e. doesn't reveal the email address, no obvious loopholes and so on.

Hi,

That kinda sounds like my basic contact form which I pull out of my library stack for any client requiring no-frills contact functionality. I'll give you the vanilla code before I make client specific changes.

It's pretty well commented. Though, you will probably want to change the markup in the form itself as it crosses over into some style code which is not relevant to you.

Please note: it's structured as an add-on module for my development framework. If you want to call it within a 'flat' page, just add the BuildContactPanel() function call within the markup where you want the form and response message to go. Yes, this script points to itself, so, you are posting to the page the code is on.

Enjoy!

<?php
//contact.php
//Contact module file
//Design by David Thomson.

//Builds the markup for the contact form.
// * Return:Panel of contact form an confirm
function BuildContactPanel()
{
//Get webpage template.
$Str_ContactPanel = '';

//Evaluate if contact form has been submitted.
$Bol_ContactForm = (isset($_POST['contact_flag']) && $_POST['contact_flag'] == '1')? true: false;

if ($Bol_ContactForm)
{
//Declare form post variables.
$Str_ContactFlag = (isset($_POST['contact_flag']))? $_POST['contact_flag']: '';
$Str_ContactName = (isset($_POST['contact_name']))? $_POST['contact_name']: '';
$Str_ContactEmail = (isset($_POST['contact_email']))? $_POST['contact_email']: '';
$Str_ContactMessage = (isset($_POST['contact_text']))? $_POST['contact_text']: '';

//Set validation flags.
$Bol_FormValidated= false;
$Bol_NameValidated = false;
$Bol_EmailValidated = false;
$Bol_TextValidated= false;

//Check for validation.
if ($Str_ContactName)
$Bol_NameValidated = true;

if ($Str_ContactEmail && GetEmailAddressIsValid($Str_ContactEmail))
$Bol_EmailValidated = true;

if ($Str_ContactMessage)
$Bol_TextValidated = true;

if ($Bol_NameValidated && $Bol_EmailValidated && $Bol_TextValidated)
$Bol_FormValidated = true;

//If email is valid send it.
if ($Bol_FormValidated)
{
//Do message formatting and send off email.
$Str_ContactMessage = FormatTextInput($Str_ContactMessage);
$Str_ContactMessage = GenerateTextOutput($Str_ContactMessage);
SendValidEmail($Str_ContactName, $Str_ContactEmail, $Str_ContactMessage, $Str_ContactFlag);
$Str_ContactPanel = GetEmailConfirmation($Str_ContactName, $Str_ContactEmail, $Str_ContactMessage);
}
//Otherwise construct form with warnings.
else
{
$Str_ContactPanel = GetContactForm($Str_ContactName, $Str_ContactEmail, $Str_ContactMessage, $Bol_NameValidated, $Bol_EmailValidated, $Bol_TextValidated, $Str_ContactFlag);
}
}
else
{
//Standard contact form output.
$Str_ContactPanel = GetContactForm('', '', '', true, true, true, '');
}

return $Str_ContactPanel;
}

//Simple regular expression test for an email address.
// - $Str_InputEmail:Email address to validate.
// * Return:False if locally held link input is not a valid email address, otherwise true.
function GetEmailAddressIsValid($Str_InputEmail)
{
if (!eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $Str_InputEmail))
return false;

return true;
}

//Removes excess special characters from $Str_TextInput before converting to a well formed string.
// - $Str_TextInput:Form input text to be formatted
// * Return:String with excess special characters removed, ready for xml format
function FormatTextInput($Str_TextInput)
{
//Declare return string.
$Str_FormattedString = $Str_TextInput;

//Remove excess carriage returns.
$Int_DblReturn = TRUE;
while ($Int_DblReturn !== FALSE)
{
$Str_FormattedString = str_replace("\r\r", "\r", $Str_FormattedString);
$Int_DblReturn = strpos($Str_FormattedString, "\r\r");
}

//Remove excess spaces.
$Int_DblSpace = TRUE;
while ($Int_DblSpace !== FALSE)
{
$Str_FormattedString = str_replace(' ', ' ', $Str_FormattedString);
$Int_DblSpace = strpos($Str_FormattedString, ' ');
}

//Cleanup special characters.
$Str_FormattedString = str_replace("\t", "", $Str_FormattedString);
$Str_FormattedString = str_replace("\n", "", $Str_FormattedString);

return $Str_FormattedString;
}

//Adds paragraphs tags to each item in a string array.
// - $Arr_InputTextParagraphs:Array of strings each item representing one paragraph
// * Return:Single string as a sequence of markup paragraphs
function GenerateTextOutput($Str_TextToOutput)
{
//Declare return string.
$Str_FormattedOutput = '';

$Arr_InputTextParagraphs = explode("\r", $Str_TextToOutput);

//Add each paragraph.
foreach ($Arr_InputTextParagraphs as $Str_Paragraph)
{
if ($Str_Paragraph != '')
$Str_FormattedOutput .= '<p>'.$Str_Paragraph.'</p>';
}

return $Str_FormattedOutput;
}

//Formats and send an email form the contact form.
// - $Str_InputName:Formatted name posted by contact form
// - $Str_InputEmail:Formatted email posted by contact form
// - $Str_InputText:Formatted message text posted by contact form
// * Return:VOID
function SendValidEmail($Str_InputName, $Str_InputEmail, $Str_InputText)
{
//Email variables.
$Str_EmailAddress= 'someone@somewhere.com';
$Str_EmailSubject= 'Email Subject Line';
$Str_EmailSender= 'Organisation Name';

//Block invalid headings.
$Arr_IllegalHeaders = array("/to\:/i", "/from\:/i", "/bcc\:/i", "/cc\:/i", "/Content\-Transfer\-Encoding\:/i", "/Content\-Type\:/i", "/Mime\-Version\:/i");
$Str_InputText = preg_replace($Arr_IllegalHeaders, '', $Str_InputText);

//Format message string.
$Str_FormattedEmail = '<html>'."\r\n"
.'<head>'."\r\n"
.'<title>'.$Str_EmailSender.'</title>'."\r\n"
.'</head>'."\r\n"
.'<body style="font-family: verdana, arial, serif; font-size: 10pt;">'."\r\n"
.'Sender: '.$Str_EmailSender.'<br />'."\r\n"
.'From: '.$Str_InputName.'<br />'."\r\n"
.'Email: '.$Str_InputEmail.'<br />'."\r\n"
.$Str_InputText."\r\n"
.'</body>'."\r\n"
.'</html>';

$Str_EmailHeader= 'MIME-Version: 1.0'."\r\n"
.'Content-type: text/html; charset=iso-8859-1'."\r\n"
.$Str_EmailSender."\r\n";

//Send email.
mail($Str_EmailAddress, $Str_EmailSubject, $Str_FormattedEmail, $Str_EmailHeader);
return;
}

//Outputs contact confirmation markup.
// * Return:Contact confirmation markup code string
function GetEmailConfirmation($Str_InputName, $Str_InputEmail, $Str_InputText)
{
//Declare return string.
$Str_ContactConfirmation = '<h3>Your Email Has Been Sent</h3>'
."\n".'<p>Here is confirmation of the email you just sent.</p>'
."\n\t".'<dl>'
."\n".'<dt>Name:</dt>'
."\n".'<dd class="confirm">'.$Str_InputName.'</dd>'
."\n".'<dt>Email:</dt>'
."\n".'<dd class="confirm">'.$Str_InputEmail.'</dd>'
."\n".'<dt>Message:</dt>'
."\n".'<dd class="confirm">'.$Str_InputText.'</dd>'
."\n\t".'</dl>'
."\n".'<p class="hang-2">We&#39;ll will be in touch at the nearest possible convenience&#46;</p>'
."\n".'<p>Regards,</p>'
."\n".'<p>Name of contacted person</p>';

return $Str_ContactConfirmation;
}

//Returns a default contact form as a string.
// - $Str_InputName:Formatted name posted by contact form
// - $Str_InputEmail:Formatted email posted by contact form
// - $Str_InputText:Formatted message text posted by contact form
// - $Bol_NameValid:Boolean name validation value
// - $Bol_EmailValid:Boolean email validation value
// - $Bol_TextValid:Boolean message validation value
// - $Str_RadioChecked:Checked radio vaule as a string
// * Return:Default contact form as a string
function GetContactForm($Str_InputName, $Str_InputEmail, $Str_InputText, $Bol_NameValid, $Bol_EmailValid, $Bol_TextValid, $Str_RadioChecked)
{
//Error warnings.
$Str_NameErrorString = ($Bol_NameValid)? '': '<span class="warning">Name needed please.</span><br />';
$Str_EmailErrorString = ($Bol_EmailValid)? '': '<span class="warning">Valid email address required.</span><br />';
$Str_TextErrorString= ($Bol_TextValid)? '': '<span class="warning">Please enter your message.</span><br />';

//Warning style.
$Str_NameWarningStyle= ($Bol_NameValid)? '3': '4';
$Str_EmailWarningStyle= ($Bol_EmailValid)? '3': '4';
$Str_TextWarningStyle= ($Bol_TextValid)? '19': '20';

//Declare return string.
$Str_ContactForm = '<form id="contact" action="http://www.domainname.com/contact" method="post">'
.'<fieldset>'
.'<input type="hidden" name="contact_flag" value="1" id="contact_flag" />'
.'<ul class="push-0 pull-0">'
.'<li class="stretch-'.$Str_NameWarningStyle.'"><label for="contact_name">Name:</label>'.$Str_NameErrorString.'<input type="text" value="'.$Str_InputName.'" id="contact_name" name="contact_name" class="txt span-5" /></li>'
.'<li class="stretch-'.$Str_EmailWarningStyle.'"><label for="contact_email">Email:</label>'.$Str_EmailErrorString.'<input type="text" value="'.$Str_InputEmail.'" id="contact_email" name="contact_email" class="txt span-5" /></li>'
.'<li class="stretch-'.$Str_TextWarningStyle.'"><label for="contact_text">Message:</label>'.$Str_TextErrorString.'<textarea id="contact_text" name="contact_text" class="span-6 stretch-15">'.$Str_InputText.'</textarea></li>'
.'<li class="stretch-1 hang-1 text-c"><input type="submit" name="submit" id="submit_button" value="Send Message" class="" alt="Submit this form" /></li>'
.'</ul>'
.'</fieldset>'
.'</form>';

return $Str_ContactForm;
}

?>

PS: I'm not using code style as it appears broken...

apprentice

12:24 am on Mar 30, 2008 (gmt 0)

10+ Year Member



Hi Dave75,

Thank you so much for your contribution. I will definitely have a go at it - in a couple of days time, as I 'm stuck with other work at the moment. I will post back for any impressions and/or questions that I might have.

Once again, thanks alot!