Jump to content

Adding new field to contact form


Stefan83

Recommended Posts

Hi,

I've added a new field (location) to my contact form and it appears as it should, however, when an email is sent from the form, the content from this new field does not show in the email when it is delivered. Can anyone point out what the problem is in the following code? Thanks in advance!

 

<?php

// User settings
$to = "mail@stefanlesik.com";
$subject = "Cambridge Stained Glass";

// Include extra form fields and/or submitter data?
// false = do not include
$extra = array(
   "form_subject"   => true,
   "form_cc"      => true,
   "ip"            => false,
   "user_agent"   => false
);

// Process
$action = isset($_POST["action"]) ? $_POST["action"] : "";
if (empty($action)) {
   // Send back the contact form HTML
   $output = "<div style='display:none'>
   <div class='contact-top'></div>
   <div class='contact-content'>
      <h1 class='contact-title'>Send us a message:</h1>
      <div class='contact-loading' style='display:none'></div>
      <div class='contact-message' style='display:none'></div>
      <form action='#' style='display:none'>
         <label for='contact-name'>*Name:</label>
         <input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' />
         <label for='contact-email'>*Email:</label>
         <input type='text' id='contact-email' class='contact-input' name='email' tabindex='1002' />
         <label for='contact-location'>Location:</label>
         <input type='text' id='contact-location' class='contact-input' name='location' tabindex='1003' />";

   if ($extra["form_subject"]) {
      $output .= "
         <label for='contact-subject'>Subject:</label>
         <input type='text' id='contact-subject' class='contact-input' name='subject' value='' tabindex='1004' />";
   }

   $output .= "
         <label for='contact-message'>*Message:</label>
         <textarea id='contact-message' class='contact-input' name='message' cols='40' rows='4' tabindex='1005'></textarea>
         <br/>";

   if ($extra["form_cc"]) {
      $output .= "
         <label> </label>
         <input type='checkbox' id='contact-cc' name='cc' value='1' tabindex='1005' /> <span class='contact-cc'>Send me a copy</span>
         <br/>";
   }

   $output .= "
         <label> </label>
         <button type='submit' class='contact-send contact-button' tabindex='1006'>Send</button>
         <button type='submit' class='contact-cancel contact-button simplemodal-close' tabindex='1007'>Cancel</button>
         <br/>
         <input type='hidden' name='token' value='" . smcf_token($to) . "'/>
      </form>
   </div>
</div>";

   echo $output;
}
else if ($action == "send") {
   // Send the email
   $name = isset($_POST["name"]) ? $_POST["name"] : "";
   $email = isset($_POST["email"]) ? $_POST["email"] : "";
   $subject = isset($_POST["subject"]) ? $_POST["subject"] : $subject;
   $location = isset($_POST["location"]) ? $_POST["location"] : "";
   $message = isset($_POST["message"]) ? $_POST["message"] : "";
   $cc = isset($_POST["cc"]) ? $_POST["cc"] : "";
   $token = isset($_POST["token"]) ? $_POST["token"] : "";

   // make sure the token matches
   if ($token === smcf_token($to)) {
      smcf_send($name, $email, $subject, $location, $message, $cc);
      echo "Your message was successfully sent.";
   }
   else {
      echo "Unfortunately, your message could not be verified.";
   }
}

function smcf_token($s) {
   return md5("smcf-" . $s . date("WY"));
}

// Validate and send email
function smcf_send($name, $email, $subject, $location, $message, $cc) {
   global $to, $extra;

   // Filter and validate fields
   $name = smcf_filter($name);
   $subject = smcf_filter($subject);
   $email = smcf_filter($email);
   $location = smcf_filter($location);
   if (!smcf_validate_email($email)) {
      $subject .= " - invalid email";
      $message .= "\n\nBad email: $email";
      $email = $to;
      $cc = 0; // do not CC "sender"
   }

   // Add additional info to the message
   if ($extra["ip"]) {
      $message .= "\n\nIP: " . $_SERVER["REMOTE_ADDR"];
   }
   if ($extra["user_agent"]) {
      $message .= "\n\nUSER AGENT: " . $_SERVER["HTTP_USER_AGENT"];
   }

   // Set and wordwrap message body
   $body = "From: $name\n\n";
   $body .= "Message: $message";
   $body = wordwrap($body, 70);

   // Build header
   $headers = "From: $email\n";
   if ($cc == 1) {
      $headers .= "Cc: $email\n";
   }
   $headers .= "X-Mailer: PHP/SimpleModalContactForm";

   // UTF-8
   if (function_exists('mb_encode_mimeheader')) {
      $subject = mb_encode_mimeheader($subject, "UTF-8", "B", "\n");
   }
   else {
      // you need to enable mb_encode_mimeheader or risk 
      // getting emails that are not UTF-8 encoded
   }
   $headers .= "MIME-Version: 1.0\n";
   $headers .= "Content-type: text/plain; charset=utf-8\n";
   $headers .= "Content-Transfer-Encoding: quoted-printable\n";

   // Send email
   @mail($to, $subject, $body, $headers) or 
      die("Unfortunately, a server issue prevented delivery of your message.");
}

// Remove any un-safe values to prevent email injection
function smcf_filter($value) {
   $pattern = array("/\n/","/\r/","/content-type:/i","/to:/i", "/from:/i", "/cc:/i");
   $value = preg_replace($pattern, "", $value);
   return $value;
}

// Validate email address format in case client-side validation "fails"
function smcf_validate_email($email) {
   $at = strrpos($email, "@");

   // Make sure the at (@) sybmol exists and  
   // it is not the first or last character
   if ($at && ($at < 1 || ($at + 1) == strlen($email)))
      return false;

   // Make sure there aren't multiple periods together
   if (preg_match("/(\.{2,})/", $email))
      return false;

   // Break up the local and domain portions
   $local = substr($email, 0, $at);
   $domain = substr($email, $at + 1);


   // Check lengths
   $locLen = strlen($local);
   $domLen = strlen($domain);
   if ($locLen < 1 || $locLen > 64 || $domLen < 4 || $domLen > 255)
      return false;

   // Make sure local and domain don't start with or end with a period
   if (preg_match("/(^\.|\.$)/", $local) || preg_match("/(^\.|\.$)/", $domain))
      return false;

   // Check for quoted-string addresses
   // Since almost anything is allowed in a quoted-string address,
   // we're just going to let them go through
   if (!preg_match('/^"(.+)"$/', $local)) {
      // It's a dot-string address...check for valid characters
      if (!preg_match('/^[-a-zA-Z0-9!#$%*\/?|^{}`~&\'+=_\.]*$/', $local))
         return false;
   }

   // Make sure domain contains only valid characters and at least one period
   if (!preg_match("/^[-a-zA-Z0-9\.]*$/", $domain) || !strpos($domain, "."))
      return false;   

   return true;
}

exit;

?>

Link to comment
Share on other sites

you never add the location variable to your header, or message body, or really anything having to do with the email. If you want the location to be added to the message body, do something like

 

$body .= "Location: $location";

 

I haven't done php mail in a while, so i'm not sure if there is a special place Location is supposed to go, but I think thats right. Hope that helps!

 

oh and btw, wrap your code with code or php tags IE: "<php>code</php> or <code>code</code>" but instead of < and > characters replace them with [ and ] characters respectively

Link to comment
Share on other sites

Thanks for your quick reply! I've just tried your solution and it didn't work unfortunately.

 

Previously I just copied the name field and replaced it with 'location'

<label for='contact-location'>Location:</label>
         <input type='text' id='contact-location' class='contact-input' name='location' tabindex='1003' />

and then added 'location' to wherever seemed necessary in the code. eg.

$location = isset($_POST["location"]) ? $_POST["location"] : "";

 

As I was just copying what was already there I assumed it could be easy to add another field but I guess PHP isn't that simple! Any ideas? Thanks

Link to comment
Share on other sites

yeah thats fine, but the problem is you aren't adding the location variable to your email anywhere. you are going to have to alter your email function and add it somewhere.

 

Exactly what is the location field? And where do you want it to be in the email?

Link to comment
Share on other sites

Thanks mikesta707, really appreciate your help on this!

 

The location (have now decided to change it to post code) field should just be a single text box where users can enter their town/city/postcode so basically it should just function the same way as the name field. I can't see where the name variable is to replicate this if this is what I need to do. The order of the fields I would like to have is as follows:

 

Name:

Email:

Telephone: (To be added after I've added the location field)

Post Code:

Subject:

Message:

 

I hope this is all making sense :)

 

Cheers

 

Stefan

 

Link to comment
Share on other sites

Ok. I'm going to assume that in an actual email, you want the location to be somewhere near the bottom of the message itself, like

 

blah blah blah

blah blah

blah

 

101 lexinton ave, manhattan new york, 10001

 

is that correct? Well you are going to have to add the location to the text of your body. In your mail function, there are a few lines that look like:

 

// Set and wordwrap message body
$body = "From: $name\n\n";
$body .= "Message: $message";
$body = wordwrap($body, 70);

 

this is where the function creates the information for the body of the message itself. What you want to do (I'm assuming, correct me if I'm wrong) is add the location to the bottom of the email message. So that means we want to effect the Message of the $body variable. to do that, try something like

 

$body .= "Message: $message" . "\n" . $location;

instead of

$body .= "Message: $message";

 

I haven't done a mailing script in a long while, So i could be a little off. I took a look through the php.net entry for the mail() function, and I didn't see any "location" header so I believe that the message body is the only place it can go.

 

Hope that helps!

Link to comment
Share on other sites

Yes it worked many thanks!!!

 

Although I have now uploaded to the actual server I'll be using it on and the email isn't delivered at all. I've tried contacting my ISP (Streamline.net) and they have just blamed it on a 'scripting issue' which it can't be because it is already working on another server. I know they support PHP so I'm not sure what else could be the problem. Any ideas?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.