Join us on Facebook

Please wait..10 Seconds Cancel

1.03.2014

// // Leave a Comment

Make a Simple Contact Form Using PHP

Simple Contact Form
Arrange File As::
/Inc/config.inc
/contactform.php
/footer.php
/header.php
-Readme.php
-contact.php
contact.php
<?
   include('inc/config.inc');

 if ($_SERVER['REQUEST_METHOD'] == "POST") {

      $name     = $_POST[name];
      $email    = $_POST[email];
      $company  = $_POST[company];
      $phone    = $_POST[phone];
      $website  = $_POST[website];
      $msg      = $_POST[msg];
      $subject  = $_POST[subject];
      
      // $msg2 is set in config.inc which formats the body of the message
    mail("$adminemail[$who]", "$subjectheader $subject", "$msg2", "From: $email \nReply-To: $email");

      if ($redirecturl != "") {
         header("Location: $redirecturl");
      } else {
         echo "<br><center>$finishedtext</center><br>";
      }

 } else {
      include($script_dir . '/inc/header.php');
    include($script_dir . '/inc/contactform.php');
      include($script_dir . '/inc/footer.php');
 }
?>

/inc/config.inc
<?

# script name
$scriptname = "Contact Form";

# use full paths
# not using full paths could prevent the script from functioning properly

# script directory # no trailing slash
$script_dir = "/home/www/ContactForm";

# script url (url to script directory above)
# no trailing slash
$script_url = "http://www.hungrydevelop.blospot.com/demo/ContactForm";

# variables below corresponds to the Email to * in your contact form
# match the numbers to the value for which option maps to a specific email
# you can add more as long as you keep them in sync

$adminemail[1] = "custservice@gmail.com";
$adminemail[2] = "information@gmail.com";
$adminemail[3] = "support@gmail.com";

# preceeds the subject the user puts in on the contact form
$subjectheader = "[Contact Form]:";

# url the form will redirect to after sending email
$redirecturl = "http://hungrydevelop.blogspot.com/";

# text that will display if you leave above variable blank
$finishedtext = "Thank you for your inquiry. We will reply ASAP!";

# how the message will show in the email
# you can reorder these how you wish or modify the message itself to your liking
# just be sure and leave the $variables in tact

$msg2 = "
    Name: $name
    Email: $email
    Company: $company
    Phone: $phone
    Website: $website
    Message:$msg
";
?>
/inc/contactform.php
<SCRIPT LANGUAGE="JavaScript">
    <!-- Hide code from non-js browsers
    function validate()
    {
        formObj = document.contact;
        if ((formObj.name.value == "") ||
            (formObj.email.value  == "") ||
            (formObj.subject.value  == "") ||
            (formObj.msg.value  == ""))
        {
            alert("You have not filled in all required fields.");
            return false;
        }
        else {
            alert('<? echo $finishedtext; ?>');
            return true;
        }
    }
    // end hiding -->
</SCRIPT>

<TABLE BORDER=0 cellpadding=5 cellspacing=0>
  <TR>
    <TD>
       <TABLE BORDER=0 cellpadding=2 cellspacing=0>
         <form action="<? echo $script_url; ?>/contact.php" method="post" name="contact" onSubmit="return validate()">
         <TR>
           <TD>
                <BR>
                <DIV>
                1. Name *<BR>
                <input type=text value='your name' size=50 maxlength=50 name=name class='txtfield'><BR>

                2. Email Adress *<BR>
                <input type='text' value='you@yourname.com' size='50' maxlength='60' name='email'><BR>

                3. Company Name<BR>
                <input type='text' value='your company' size='50' maxlength='50' name='company'><BR>

                4. Phone Number<BR>
                <input type='text' value='(000) 000-0000' size='50' maxlength='32' name='phone'><BR>

                5. Webiste<BR>
                <input type='text' value='http://www.yoursite.com' size='50' maxlength='60' name='website'><BR>

                6. Email to *<BR>
                <select name='who'>
                <option value='1'>Customer Service</option>
                <option value='2'>Information</option>
                <option value='3'>Technical Support</option>
                </select><BR>

                7. Subject *<BR>
                <input type='text' value='' size='50' maxlength='60' name='subject'><BR>

                8. Message *<BR>
                <textarea name='msg' rows='7' cols='50'></textarea><BR>
                </DIV>
                <input type='submit' value='submit'>* Field Required<BR>
          </TD>
        </tR>
        </FORM>
      </tABLE>
    </TD>
  </TR>
</tABLE>
/inc/footer.php
<BR><BR>{placeholder for footer html here}</center>
<BR>
<!-- Please consider linking to us if you use this script. Thanks! -->
<Div align=center><A HREF="http://hungrydevelop.blospot.com">powered by hungrydevelop.blogspot.com : <? echo $scriptname; ?></A></DIV>
<BR>
</BODY>
</HTML>
/inc/header.php
<HTML>
<HEAD>

 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 <title><? echo $scriptname; ?></title>
 <META name="description" content="">
 <META name="keywords" content="">
 <META name="revisit-after" content="7days">
 <META name="robots" content="index, follow">

</HEAD>
<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0 rightmargin=0>
<center><br>{placeholder for header html here}<br>

0 comments:

Post a Comment