PHP contact form?

Soldato
Joined
28 Sep 2008
Posts
14,216
Location
Britain
Hi guys,

Hoping this is a simple fix.

For someone reason, I don't like using the form to submit to another file. I want it to submit to itself and run the scripts.

I've created an enquiry form for a website which takes all the form and submits it to the database. I'd also like this form to email someone at the company to let them know they have a contact.

I'm not sure entirely where to start by ensuring things like bots, spam, swearwords, etc are covered off. I've seen 1 or 2 examples online but all tend to post to a seperate file which I'd like to avoid.

Here's my current code if anyone fancies helping out.

Thanks

PHP:
<?php
require_once('includes/config.php');

$showform = true;
$showmessage = false;

if (isset($_POST['submit'])) {

    $form = new wc_validation();

    $form->Text($_POST['Name'],'Name',false);
    $form->Text($_POST['Callback'],'Callback',false);
    $form->Text($_POST['Telephone'],'Telephone',false);
    $form->Text($_POST['Email'],'Email',false);
    $form->Text($_POST['Enquiry'],'Enquiry',false);
    $form->Text($_POST['Hearabout'],'Hearabout',false);
    $form->Text($_POST['Company'],'Company',true);

    if (!$form->hasErrors){

       $sql = "INSERT INTO contacts (Name, Callback, Telephone, Email, Enquiry, Hearabout, Company) VALUES
        ('" . $form->formData['Name']. "','" . $form->formData['Callback']. "',
        '" . $form->formData['Telephone']. "','" . $form->formData['Email']. "',
        '" . $form->formData['Enquiry']. "','" . $form->formData['Hearabout']. "',
        '" . $form->formData['Company']. "')";
       if ($db->Insert($sql)){
           $showform = false;
           $showmessage = true;

       }
    }


} else {


}



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="includes/css/screen.css" media="screen" />
<script type="text/javascript" src="includes/js/imagerotatefade.js"></script>
</head>

<body onload="runAll()">
<div id="wrapper"><!--start wrapper-->
<div id="topbar">
<a href="" border="0"><img src="includes/images/top.png" alt="" name='SlideShow' /></a>
</div>
<div id="header">
<!--start header--><img src="includes/images/head1.png" alt="" name='ImgShow' /></div><!--end header-->
<div id="content">
<ul id="nav"><!--start navigation-->	

</ul>
<!--end nav-->

<div id="main-content"><!--start main-content-->

<?php if ($showform){
?>
<h1>How to contact us</h1>
<div style="float:right; margin-top:10px;margin-right:10px;">
<img src="includes/images/enquiry.jpg" alt=""/>
</div>

<p>If you would like to contact us, please submit your enquiry using the form below.</p>
<p>We aim to respond to your emails and enquiries within 24 hours of receiving them.</p>
<p>Any information you pass onto us is governed by the 1998 Data Protection Act</p>

<h2>Enquiry Form</h2>
<br />

<?php if ($form){

    /* if the form object exists which it will after submission put the data into an array to display stuff to user. */
    $data['callback'] = $form->formData['Callback'];
    $data['name'] = $form->formData['Name'];
    $data['telephone'] = $form->formData['Telephone'];
    $data['email'] = $form->formData['Email'];
    $data['hearabout'] = $form->formData['Hearabout'];
    $data['enquiry'] = $form->formData['Enquiry'];
    $data['company'] = $form->formData['Company'];

    echo $form->DisplayErrors();
}
?>


<form id="enquiryform" action="" method="post">
	<p><label>Name:</label><input type="text" class="required" id="Name" name="Name" maxlength="100" value="<?php echo $data['name']?>" /><span class="warning">*</span></p>
	<p><label>Company:</label><input type="text" class="required" id="Company" name="Company" maxlength="200" value="<?php echo $data['company']?>" /><span class="warning">*</span></p>
	<p><label>Telephone:</label><input type="text" class="required" id="Telephone" name="Telephone" maxlength="200" value="<?php echo $data['telephone']?>" /><span class="warning">*</span></p>
	<p><label>Email:</label><input type="text" class="required" id="Email" name="Email" maxlength="60" value="<?php echo $data['email']?>" /><span class="warning">*</span></p>
	<p><label>Enquiry:</label><textarea rows="10" cols="50" name="Enquiry" /><?php echo $data['enquiry']?></textarea></p>
	<p><label>Please call me back:</label>
		<select name="Callback" id="Callback" />
			<option value='0'>Please Select :
</option>
            <option value="1" <?php if ($data['callback'] == '1') echo "selected='selected'"; ?>>Anytime</option>
			<option value="2" <?php if ($data['callback'] == '2') echo "selected='selected'"; ?>>Morning</option>
			<option value="3" <?php if ($data['callback'] == '3') echo "selected='selected'"; ?>>Afternoon</option>
			<option value="4" <?php if ($data['callback'] == '4') echo "selected='selected'"; ?>>Evening</option>
			<option value="5" <?php if ($data['callback'] == '5') echo "selected='selected'"; ?>>No</option>
		</select></p>
		<p><label>Where did you hear about us?:</label>
		<select name="Hearabout" id="Hearabout" />
			<option value='0'>Please Select :
</option>
            <option value="1" <?php if ($data['hearabout'] == '1') echo "selected='selected'"; ?>>Already a client</option>
			<option value="2" <?php if ($data['hearabout'] == '2') echo "selected='selected'"; ?>>Referred by a company</option>
			<option value="3" <?php if ($data['hearabout'] == '3') echo "selected='selected'"; ?>>Magazine / Newspaper Advert</option>
			<option value="4" <?php if ($data['hearabout'] == '4') echo "selected='selected'"; ?>>Search Engine</option>
			<option value="5" <?php if ($data['hearabout'] == '5') echo "selected='selected'"; ?>>Other</option>
		</select></p>
			<p><input type="submit" class="button" name="submit" value="Submit Enquiry" /></p>
		</form>
   <p>Fields marked with an asterisk (<span class="warning">*</span>) must be completed.</p>
<?php } else if($showmessage){ ?>
<h1>Your Enquiry</h1>
<p>Thank you. You have successfully submitted an enquiry. One of our consultants will be in contact with you shortly. To return to the Overview Page, click <a href="index.php">here</a></p>




<?php } ?>



<?php

if($error) echo $error;


?>

</div><!--end maincontent-->
</div><!--end content-->
<p style="font-size:1px;line-height:1px;margin:0;padding:0;clear:both;">&nbsp;</p>
</div><!--end wrapper-->
<div id="bottom-links">
<div id="lefttext">
&copy;2011 <a href="#"></a> All rights reserved.</div>
<div id="righttext">
<a href="terms-and-conditions.html">Terms &amp; Conditions</a>&nbsp;|&nbsp;<a href="site-map.html">Site Map</a>
</div>
</div>
</body>
</html>
 
I found the best method for bots was to put in a field called something like 'email' that was hidden by using inline styles. Thereby, normal users don't see it and don't fill it in where as a bot reads the page, fills every field in (especially one called email). However with some form validation you can check that field is not empty and send them off else where and not send the form. Where as a normal user on clicking submit would pass that validation and send the form.
 
Last edited:
Back
Top Bottom