Contact Form Validation

This is a discussion on "Contact Form Validation" within the PHP Forum section. This forum, and the thread "Contact Form Validation are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Nov 21st, 2007, 14:02
Reputable Member
Join Date: Oct 2007
Location: UK
Posts: 267
Thanks: 0
Thanked 0 Times in 0 Posts
Contact Form Validation

Hey everyone, can u have a look at my code.....I need to add some sort of validation that will display a little message underneath my 'submit' button saying "Email sent" if it was posted or "error" if it wasnt.

I dont know how to do this

Code: Select all
<?
error_reporting(E_ALL);
ini_set('display_errors', '1');
if(isset($_POST['submit'])) { 
$to = "danielm@design365.co.uk"; 
$subject = "Genetics Feedback"; 
$name_field = $_POST['name'];
$telephone_field = $_POST['telephone']; 
$email_field = $_POST['email']; 
$comments_field = $_POST['comments']; 
 
$body = "From: $name_field\n E-Mail: $telephone_field\n Telephone: $email_field\n Comments:\n $comments"; 
 
 
mail($to, $subject, $body); 
} else { 
echo "Error! Please try again"; 
}
function antispam()
{
// First, make sure the form was posted from a browser.
// For basic web-forms, we don't care about anything
// other than requests from a browser:
    if (!isset($_SERVER['HTTP_USER_AGENT'])) {
        die("Forbidden - You are not authorized to view this page");
        exit;
    }
// Make sure the form was indeed POST'ed:
    //  (requires your html form to use: action="post")
    if (!$_SERVER['REQUEST_METHOD'] == "POST") {
        die("Forbidden - You are not authorized to view this page");
        exit;
    }
// Make sure the form was posted from an approved host name.
    if (!strstr($_SERVER['HTTP_REFERER'], "cashincar")) {
        //logBadRequest();
        header("HTTP/1.0 403 Forbidden");
        exit;
    }
// Attempt to defend against header injections:
    $badStrings = array("Content-Type:",
    "MIME-Version:",
    "Content-Transfer-Encoding:",
    "bcc:",
    "cc:",
    "http://www.genetics.co.uk/contact.php");
// Loop through each POST'ed value and test if it contains
    // one of the $badStrings:
    foreach($_POST as $k => $v){
        foreach($badStrings as $v2){
            if (strpos($v, $v2) !== false) {
                //        logBadRequest();
                header("HTTP/1.0 403 Forbidden");
                exit;
            }
        }
 
    }
 
}
/*// do stuff to the post
if ($_POST) {
antispam();
while (list($key, $value) = each($_POST)) {
$$key = $value;
$message .=$key.": ".$value."\n";
}
//mail form    
$message = "There has been a message/request from ".$_SERVER['HTTP_HOST']."\n\n".$message;
$message .="-----------------------\n\n";
$message = stripslashes($message);
mail("danielm@design365.co.uk", "Genetics Feedback", $message, "From: danielm@design365.co.uk");
}*/ 
?>
<!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>geNetics Web - Next Generation Web Development</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="gen.css">
</head>
<body>
<div id="wrapper">
  <div id="about_us">
    <ul>
      <li><a href="index.html"> Home</a></li>
      <li><a href="our_team.html">Our team</a></li>
      <li><a href="about_us.html">About us</a></li>
      <li><a href="our_work.html">Our work</a></li>
      <li><a href="contact.php"class="yourhere">Contact</a></li>
    </ul>
 
 
 
 <p>Please don't hesitate to contact us, we will be more than happy to help. Simply fill out the form below:</p>
<div class="form"><form name="contact" action="contact.php" method="POST">
  Your Name:<br>
  <input class="form_style" name="name" type="text" size="20" maxlength="30"/><br><br>
  Telephone:<br>
  <input class="form_style" name="telephone" type="text" size="20" maxlength="20" /><br><br>
  Email:<br>
  <input class="form_style" name="email" type="text" size="20" maxlength="50" /><br><br>
  Comments and Questions:<br>
     <textarea class="form_style" name="comments" cols="20" rows="5"></textarea><br>
   <input class="submitbtn" name="submit" type="submit" value="Submit"/>
   </form>
   <?echo "Data has been submitted to $to!";?>
   </div>
</div>
 
  <div id="footer">Next generation web development</div> 
</body>
</html>
Reply With Quote

  #2 (permalink)  
Old Nov 21st, 2007, 15:29
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Contact Form Validation

Try this code:
PHP: Select all

 if(mail($to$subject$body)) {
 echo 
"Sent";
 } else {
 echo 
"Mail failed";
 } 
Also try to open and clsoe tags like the following in case you move to a server that doesn't support short tags!
PHP: Select all

<?php 
// code
?>
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] PHP contact form redirect to same form Posie PHP Forum 14 Jan 29th, 2008 20:28
php contact form geyids PHP Forum 5 Jun 14th, 2007 16:59
what is the best contact form Phixon Web Page Design 10 May 15th, 2007 18:47
Contact us form help! NurseJenny PHP Forum 10 Jun 23rd, 2006 13:27
Need Quote Form and Contact Form Av8er Flash & Multimedia Forum 5 Oct 30th, 2003 17:14


All times are GMT. The time now is 02:07.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43