FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


For those who need a contact form





bladesage
Hello!

I've seen many topics out there about people looking for contact forms. Just simple contact forms, everyone seems to want one. So I decided to make things a bit easier for everyone, and provide a really nice contact form.

Simply copy the following code, save it as whatever.php, and set the variables to suit your needs. The variables are all labeled with comments (//).
Code:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Contact</title>
</head>

<?php

/****************************************************************************************************
This is the Simple Contact form by bladesage                                          *
To optimize your form's performance, edit these variables to suit your needs.                  *
Variables begin with the $ symbol, and are marked accordingly using comments (//) to their right   *
Only the first 3 variables need to be changed, the others work fine the way they are.            *
****************************************************************************************************/
$to = ""; //Change to your email address
$subject = ""; //Change to the subject of the emails you will get
$home = ""; //Change to the URL of your website
$adddate = true; //Change to false if you don't want the date added
$verify = true; //Change to false if you don't want it to verify the form

?>

<body>

<?php

/*
NOTE: Editing this may hinder the function of the contact form.
Edit this area at you own risk!
*/

if(isset($_POST['name']))
   {
   $name = $_POST['name'];
   $email = $_POST['email'];
   $subj = $_POST['subject'];
   $message = $_POST['message'];
   $date = date("l, F jS");
   $ip = $_SERVER['REMOTE_ADDR'];
   $msg = "This is the result of a form filled out by one of your visitors.\n\n";
   $msg .= "Name:\n$name\n\n";
   $msg .= "Subject:\n$subj\n\n";
   $msg .= "E-mail:\n$email\n\n";
   $msg .= "Message:\n$message\n\n\n";
   if($adddate)
      {
      $msg .= "Received:\n$date\n\n";
      }
   mail($to, $subject, $msg);
   }

else if(!isset($_POST['name']))
   {
   
?><script type="text/javascript">
<!--

var check = <?php
if($verify)
   {
   print "true;";
   }
else
   {
   print "false;";
   }
?>

function checkmail()
   {
   if(document.cntct.email.value == "")
      {
      return false;
      }
   var email = document.cntct.email.value;
   if(email.indexOf("@") == -1 || email.indexOf(".") == -1)
      {
      return false;
      }
   i=0;
   badchars = " ,'$^*#!";
   do
      {
      if(email.indexOf(badchars.charAt(i)) > -1)
         {
         return false;
         }
      i++;
      }
   while(i < badchars.length);
   return true;
   }

function verify()
   {
   if(!check)
      {
      return true;
      }
   var err = "The following required inputs have no or unacceptable values:\n\n";
   var error = false;
   if(document.cntct.name.value == "")
      {
      err += "-Name\n";
      error = true;
      }
   if(!checkmail())
      {
      err += "-Email\n";
      error = true;
      }
   if(document.cntct.subject.value == "")
      {
      err += "-Subject\n";
      error = true;
      }
   if(document.cntct.message.value == "")
      {
      err += "-Message";
      error = true;
      }
   if(error)
      {
      alert(err);
      return false;
      }
   return true;
   }

-->
</script>
<form name="cntct" action="<?php print $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return verify()">

Your name:<br />
<input type="text" name="name" size="80" /><br /><br />

Your e-mail address:<br />
<input type="text" name="email" size="80" /><br /><br />

The subject of the contact:<br />
<input type="text" name="subject" size="80" /><br /><br />

Your message:<br />
<textarea name="message" cols="80" rows="6"></textarea><br /><br />

<input type="submit" value="Send" />

</form><?php

   }

if(isset($_POST['name']))
   {
   print "Thank you for contacting us!<br /><br />";
   print "To return to the homepage, click <a href=\"$home\">here</a>.";
   }

?>

</body>
</html>


It could have been simpler I know, but I decided it could use a verification script to check the form. It will:

1) Display a form with 4 inputs (Name, Email, Subject, and Message)
2) [If $verify is set to true]Check the form values, including email
3) Either alert the user that the input values are unacceptable (and say which ones), or submit the form
4) Take all the inputs, and if the $adddate variable is set to true, add the date, into a message
5) Email it to you (as specified in the fist 2 variables)
6) Display a simple thank you screen for your users, with a link to your homepage (as indicated by the $home variable)

It seems a bit large, but it's only about 3 or 4 KB, works effectively, and works quickly.

NOTES:
-If you don't know much about PHP, it should work fine with (X)HTML and CSS, so you can feel free to change the colors, change the title, and/or add style rules if you desire

-In the PHP tags (<?php [until the] ?>), I don't reccommend changing anything other than the beginning variables, unless you know what you're doing.

Enjoy!

And if there are any problems, let me know Wink
hobbkins
This would have been perfect 5 hours ago, before I wrote mine by hand. Mine's inferior, but it was a great learning experience (I had never written a pHp script before my contact form).
DoctorBeaver
I cheat and use http://www.webformdesigner.com

It produces Jscript forms but you can easily incorporate php into them.

You can use built-in verification routines or use your own. Also, you can have the input data automatically emailed to you.
DanielXP
Making a contact form is easy and a great project to make for your first script.
Related topics
contact form
Need a simple Contact form
I am trying to build a contact form using php and flash
contact form with IP feature ?
A simple multiple choice mail form
AJAX and PHP contact form
Contact Form prob
Need a Contact form from HTML/PHP
Need help to make my Flash contact form functioning!
Easy Contact Form with Instructions
Spam protection for contact form?
Contact form or email address?
Wordpress Contact Form 7 not appearing on Custom Theme
Contact Form Issues
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.