Jump to content

PHP Form Mailer only sending 1 field


phpizza

Recommended Posts

The below php processing script and form will mail ($to, $subject, $additional_headers = "From:someVisitor.com") however $message only sends the first field.  I have concatenated the  contents of each form field into one long $message string using newline characters ( \n ) to format it. The form validation works wonderfully. If I comment out all validation it will send all fields (first name, last name, address, etc.) Does anyone know why it will only send only send the first form field?

[code]
<?php
$pattern = '/^\w[-.\w]*@([-a-z0-9]+\.)+[a-z]{2,4}$/i';
if ($_POST && array_key_exists('sendCom',$_POST)) {
  $nomessage='';
  $error=array();
  $message='';
  $message .= "First name: $_POST[fname]\n";
  $message .= "Last name: $_POST[lname]\n";
  $message .= "Address: $_POST[address]\n";
  $message .= "City: $_POST[city]\n";
  $message .= "State: $_POST[state]\n";
  $message .= "Zip Code: $_POST[zip]\n";
  $message .= "Telephone: $_POST[telephone]\n";
  $message .= "Comments: $_POST[comments]\n";
  $email='';
  $con_email='';
// Check each field and build errors array if problems found
if (isset($_POST['comments']) && !empty($_POST['comments'])) {
  $message=strip_tags($_POST['comments']);
  }
else {
  $nomessage = 'You have not entered any comments';
  }
if (isset($_POST['telephone']) && !empty($_POST['telephone'])) {
  $message=strip_tags($_POST['telephone']);
  }
else {
  $nomessage = 'Please enter your Telephone Number';
  }           
if (isset($_POST['zip']) && !empty($_POST['zip'])) {
  $message=strip_tags($_POST['zip']);
  }
else {
  $nomessage = 'Please enter your Zip Code';
  } 
if (isset($_POST['state']) && !empty($_POST['state'])) {
  $message=strip_tags($_POST['state']);
  }
else {
  $nomessage = 'Please enter your State';
  } 
if (isset($_POST['city']) && !empty($_POST['city'])) {
  $message=strip_tags($_POST['city']);
  }
else {
  $nomessage = 'Please enter your city';
  }
if (isset($_POST['address']) && !empty($_POST['address'])) {
  $message=strip_tags($_POST['address']);
  }
else {
  $nomessage = 'Please enter your address';
  }
if (isset($_POST['lname']) && !empty($_POST['lname'])) {
  $message=strip_tags($_POST['lname']);
  }
else {
  $nomessage = 'Please enter your last name';
  }
if (isset($_POST['fname']) && !empty($_POST['fname'])) {
  $message=strip_tags($_POST['fname']);
  }
else {
  $nomessage = 'Please enter your first name';
  } 
if (isset($_POST['email']) && !empty($_POST['email'])) {
  $email=trim($_POST['email']);
  }
else {
  $error['email'] = 'You have not given a return email address';
}
if (isset($_POST['con_email']) && !empty($_POST['con_email'])) {
  $con_email=trim($_POST['con_email']);
  if($email != $con_email) $error['nomatch'] = 'Your emails don\'t match';
  }
else {
  $error['confirm'] = 'Please confirm your email address';
  }
if ($email && $con_email) {
  if ($email == $con_email) {
    if (!preg_match($pattern,$email)) $error['invalid'] = 'That appears to be an invalid email address';
    }
  }
  //Create the variables for the email
$to = '[email protected]';
$subject = 'Message from  website';
$additional_headers = "From: [email protected]\n"
                                      ."Reply-To: $email";

// If no errors, send email and redirect to acknowledgment page
if (!$nomessage && !$error) {
  mail($to,$subject,$message,$additional_headers);
  header('Location: thanks.php');
  exit();
  }
}
?>
<!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>New_index</title>
<link href="styles/new.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">
  <div id="titlebar"><img src="images_construct/header/header2.jpg" alt="header" width="760" height="80" /></div>
 
    <div id="nav">
      <ul>
        <li><a href="home.php">Home</a></li>
        <li><a href="members.php">Members</a></li>
        <li><a href="committee.php">Committe</a></li>
        <li><a href="gallery.php">Gallery</a></li>
        <li><a href="contact.php">Contact</a></li>
      </ul>
    </div>
    <div id="maincontent">
<?php
// Display error message if errors have been found in submission
if (isset($nomessage) || isset($error)) {
?>
<div id="sorry"><p>Sorry, we are unable to process your request
because of errors. Please check the points highlighted in bold (red)
text, and resubmit.</p></div>
<?php
  }
?>
<h1>Send Us Your Comments </h1>
    <form id="feedback" name="feedback" method="post" action="<?php $_SERVER['PHP_SELF']?>">
      <table width="650" id="formTab">
        <tr>
          <td>&nbsp;</td>
          <td
  <?php if (isset($nomessage) && !empty($nomessage)) {
  echo 'class="error">'.$nomessage; } else { ?>
  >
  <?php } ?>
  </td>
        </tr>
        <tr>
          <td>First Name </td>
          <td colspan="2"><input value="<?php if (isset($_POST['fname'])) echo $_POST['fname']; ?>" name="fname" type="text" class="mediumbox" id="fname" /></td>
        </tr>
        <tr>
          <td>Last Name </td>
          <td colspan="2"><input value="<?php if (isset($_POST['lname'])) echo $_POST['lname']; ?>" name="lname" type="text" class="mediumbox" id="lname" /></td>
        </tr>
        <tr>
          <td>Address</td>
          <td colspan="2"><input value="<?php if (isset($_POST['address'])) echo $_POST['address']; ?>" name="address" type="text" class="mediumbox" id="address" /></td>
        </tr>
        <tr>
          <td>City</td>
          <td colspan="2"><input value="<?php if (isset($_POST['city'])) echo $_POST['city']; ?>" name="city" type="text" class="mediumbox" id="city" /></td>
        </tr>
        <tr>
          <td>State</td>
          <td colspan="2"><input value="<?php if (isset($_POST['state'])) echo $_POST['state']; ?>" name="state" type="text" class="mediumbox" id="state" /></td>
        </tr>
        <tr>
          <td>Zip Code </td>
          <td colspan="2"><input value="<?php if (isset($_POST['zip'])) echo $_POST['zip']; ?>" name="zip" type="text" class="mediumbox" id="zip" /></td>
        </tr>
        <tr>
          <td>Telephone</td>
          <td colspan="2"><input value="<?php if (isset($_POST['telephone'])) echo $_POST['telephone']; ?>" name="telephone" type="text" class="mediumbox" id="telephone" /></td>
        </tr>
        <tr>
          <td>Comments</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td colspan="3"><textarea name="comments" id="comments"><?php if (isset($_POST['comments'])) echo $_POST['comments']; ?></textarea></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td class="error">
<?php // Loop through error messages and display
foreach ($error as $key => $value) {
  echo $value.'<br />';
  }
?> </td>
        </tr>
        <tr>
          <td>Your email </td>
          <td colspan="2"><input name="email" type="text" class="mediumbox" id="email" /></td>
        </tr>
        <tr>
          <td>Confirm email </td>
          <td colspan="2"><input name="con_email" type="text" class="mediumbox" id="con_email" /></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td colspan="2"><input name="sendCom" type="submit" id="sendCom" value="Send Comments" /></td>
        </tr>
      </table>
      </form>
    <p>&nbsp;</p>
  </div>
  <div id="footer"><p>&copy; 2006 My web site</p></div>
</div>
</body>
</html>
[/code]

thanks for any help with this!

phpz
Link to comment
https://forums.phpfreaks.com/topic/34772-php-form-mailer-only-sending-1-field/
Share on other sites

Works like a charm when you  move the $message string as below:

[code]

<?php

//Create the variables for the email
$to = '[email protected]';
$subject = 'Message from  website';
$additional_headers = "From: [email protected]\n"
                                      ."Reply-To: $email";

$message='';
  $message .= "First name: $_POST[fname]\n";
  $message .= "Last name: $_POST[lname]\n";
  $message .= "Address: $_POST[address]\n";
  $message .= "City: $_POST[city]\n";
  $message .= "State: $_POST[state]\n";
  $message .= "Zip Code: $_POST[zip]\n";
  $message .= "Telephone: $_POST[telephone]\n";
  $message .= "Comments: $_POST[comments]\n";

?>

[/code]

cheers, phpz

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.