Jump to content

Adding Fields to php form


wads24

Recommended Posts

I tried to add 3 fields to this form, but can't seem to get it to work.  Can someone take a peak at it to see if they can tell me what I am doing wrong?  Thanks in advance

 

<?php

// Define your email address - where to send messages - here
define("MAIL_TARGET","[email protected]");

// Here you can redefine error messages
define("errorName","Invalid name! It must be at least 2 characters long");
define("errorEmail","Invalid email address!");
define("errorCompany","Invalid Company Name! If none... Put N/A in field.");
define("errorAddress","Invalid address!");
define("errorPhone","Invalid phone!");
define("errorMsg","Invalid message! It must be at least 10 characters long");

function createForm($name="",$email="",$company="",$address="",$phone="",$message="",$error1="",$error2="",$error3="",$error4="",$error5="",$error6=""){
?>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <table>
          <tr><td>Name: </td><td class="error"><?php echo $error1; ?></td></tr>
          <tr><td colspan="2"><input class="text" type="text" name="name" value="<?php echo $name; ?>"></td></tr>
          <tr><td>Email:</td><td class="error"><?php echo $error2; ?></td></tr>
          <tr><td colspan="2"><input class="text" type="text" name="email" value="<?php echo $email; ?>"></td></tr>
          <tr><td>Company:</td><td class="error"><?php echo $error3; ?></td></tr>
          <tr><td colspan="2"><input class="text" type="text" name="company" value="<?php echo $company; ?>"></td></tr>
          <tr><td>Address:</td><td class="error"><?php echo $error4; ?></td></tr>
          <tr><td colspan="2"><input class="text" type="text" name="address" value="<?php echo $address; ?>"></td></tr>
          <tr><td>Phone:</td><td class="error"><?php echo $error5; ?></td></tr>
          <tr><td colspan="2"><input class="text" type="text" name="phone" value="<?php echo $phone; ?>"></td></tr>
          <tr><td>Message:</td><td class="error"><?php echo $error6; ?></td></tr>
          <tr><td colspan="2"><textarea cols="40" rows="9" name="message"><?php echo $message; ?></textarea></td></tr>
          <tr><td colspan="2"><br/><input class="text" type="submit" name="submitBtn" value="Send"></td></tr>
        </table>
      </form>
<?php
}
   
function isValidEmail($email){
   $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
     
   if (eregi($pattern, $email)){
      return true;
   }
   else {
      return false;
   }   
}

function sendMail($name,$email,$company,$address,$phone,$message){
    
    $subject = "rtfarms.com Feedback Form";
    $from    = "From: $name <$email>\r\nReply-To: $email\r\n"; 
    $header  = "MIME-Version: 1.0\r\n"."Content-type: text/html; charset=iso-8859-1\r\n";
    $content = htmlspecialchars($message);
    
    $content = wordwrap($content,70);
    mail(MAIL_TARGET,$subject,$content,$from.$header);

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <title>Reggear Farms Contact Form</title>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body topmargin="0" leftmargin="0">
    <div id="main">
      <div id="caption">Contact us</div>
      <div id="icon"> </div>
<?php if (!isset($_POST['submitBtn']))  { 
    createForm();
} else  { 
      $name    = isset($_POST['name']) ? $_POST['name'] : "";
      $email   = isset($_POST['email']) ? $_POST['email'] : "";
      $company    = isset($_POST['company']) ? $_POST['company'] : "";
      $address   = isset($_POST['address']) ? $_POST['address'] : "";
      $phone   = isset($_POST['phone']) ? $_POST['phone'] : "";
      $message = isset($_POST['message']) ? $_POST['message'] : "";
      
      $error = false;
      
      if (strlen($name)<2) {
          $error = true;
          $error1 = errorName;
      }
      if (!isValidEmail($email)) {
          $error = true;
          $error2 = errorEmail;
      }
      if (strlen($company)<2) {
          $error = true;
          $error3 = errorCompany;
      }
      if (strlen($address)<2) {
          $error = true;
          $error4 = errorAddress;
      }
      if (strlen($phone)<2) {
          $error = true;
          $error5 = errorPhone;
      }
      if (strlen($message)<10) {
          $error = true;
          $error6 = errorMsg;
      }
    
      if ($error){
         createForm($name,$email,$phone,$message,$error1,$error2,$error3,$error4,$error5,$error6); 
      }
      else {
          sendMail($name,$email,$company,$address,$phone,$message);
    ?>
      
      <div id="result">
        <table width="100%">
          <tr><td>
            Thanks for your message!
          </td></tr>
        </table>
     </div>
<?php            
    }
}
?>
    <div>
</body>   

Link to comment
https://forums.phpfreaks.com/topic/200792-adding-fields-to-php-form/
Share on other sites

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.