cnh
cnh

Reputation: 17

PHP form process error

I've gotten help from this site lots of times, but this is the first time I need to post my own specifics.

I have a PHP registration form, and I'm sure it's a minor problem, but I don't know exactly what I need to fix. So, I'd appreciate somebody glancing over it.

    <?php
    if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "Form Submitted";
$email_from = "[email protected]";

function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

// validation expected data exists
if(!isset($_POST['Agency']) ||
    !isset($_POST['AgencyPhone'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');      
}

$Agency = $_POST['Agency']; // required
$AgencyPhone = $_POST['AgencyPhone']; // required
$contact = $_POST['contact']; // not required
...this goes on for quite some time...
$Zip = $_POST['Zip']; // not required

$error_message = "";
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$Agency)) {
$error_message .= 'The Agency Name you entered does not appear to be valid.<br />';
}
if(strlen($AgencyPhone) < 2) {
$error_message .= 'The Agency Phone you entered does not appear to be valid.<br />';
}
$email_message = "You received a message from your site!\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message="
Class: ".$contact."
Dates: ".$Dates." through ".$through."
Location: ".$Location."
Fee: ".$Fee.", ".$Rate."

Students:
1: ".$Last1.", ".$First1.", ".$Email1.", ".$Phone1."\n;
2: ".$Last2.", ".$First2.", ".$Email2.", ".$Phone2."\n;
3: ".$Last3.", ".$First3.", ".$Email3.", ".$Phone3."\n;
4: ".$Last4.", ".$First4.", ".$Email4.", ".$Phone4."\n;
5: ".$Last5.", ".$First5.", ".$Email5.", ".$Phone5."\n;

Agency: ".$Agency.";
Mailing: ".$Mailing.";
City: ".$City.";
Zip: ".$Zip.";
Agency Phone: ".$AgencyPhone.";
"

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>

...Then, I have the content of the process page...


<!-- Do not change anything below this line -->

<?php 
}    
?>

So, the error is in this line.

$headers = 'From: '.$email_from."\r\n".

Or, an error is, anyway. What's missing, or what do I need to add? Thanks for whatever help you can give. I really appreciate it.

My form is:

<form action="Rprocess.php" method="post" enctype="multipart/form-data" class="body">
  <p><span class="title">Enter Class Date, Location and Fee</span><br />
        <span class="style3">for which you are registering</span>:</p>
      <p align="left" class="style1">Dates:
        <input type="text" name="Dates" size="15" id="Dates" />
      through
      <input type="text" name="through" size="15" id="through" />
      </p>
      <p align="left" class="style1">Location: 
        <input type="text" name="Location" size="48" id="Location" />
      </p>
      <div class="field form-inline radio2">
      <table width="100%" border="0" cellspacing="2">
          <tr>
          <td width="509"><span class="style1">Registration Fee:
              <input name="Fee" type="text" id="Fee" value="$" size="15" />
per student</span></td>
          </tr>
      </table>
      <p class="style1">Please fill in fields below.<br />
        <span class="style3">(Confirmation e-mail will be forwarded to attendee and Registrant Agency upon receipt.)</span></p>
      <p><span class="title">Student Information</span>:</p>
      <table width="100%" border="0" cellspacing="2">
        <tr>
          <td width="25%"><span class="style1">Last</span></td>
          <td><span class="style1">First</span></td>
          <td width="25%"><span class="style1">E-mail</span></td>
          <td width="25%"><span class="style1">Phone</span></td>
        </tr>
        <tr>
          <td width="25%"><span class="style1">
            <input type="text" name="Last1" size="15" id="Last1" />
          </span></td>
          <td><span class="style1">
            <input type="text" name="First1" size="15" id="First1" />
          </span></td>
          <td width="25%"><span class="style1">
            <input type="text" name="Email1" size="15" id="Email1" />
          </span></td>
          <td width="25%"><span class="style1">
            <input type="text" name="Phone1" size="15" id="Phone1" />
          </span></td>
        </tr>
        <tr>
          <td><span class="style1">
            <input type="text" name="Last2" size="15" id="Last2" />
          </span></td>
          <td><span class="style1">
            <input type="text" name="First2" size="15" id="First2" />
          </span></td>
          <td><span class="style1">
            <input type="text" name="Email2" size="15" id="Email2" />
          </span></td>
          <td><span class="style1">
            <input type="text" name="Phone2" size="15" id="Phone2" />
          </span></td>
        </tr>
        <tr>
          <td><span class="style1">
            <input type="text" name="Last3" size="15" id="Last3" />
          </span></td>
          <td><span class="style1">
            <input type="text" name="First3" size="15" id="First3" />
          </span></td>
          <td><span class="style1">
            <input type="text" name="Email3" size="15" id="Email3" />
          </span></td>
          <td><span class="style1">
            <input type="text" name="Phone3" size="15" id="Phone3" />
          </span></td>
        </tr>
        <tr>
          <td><span class="style1">
            <input type="text" name="Last4" size="15" id="Last4" />
          </span></td>
          <td><span class="style1">
            <input type="text" name="First4" size="15" id="First4" />
          </span></td>
          <td><span class="style1">
            <input type="text" name="Email4" size="15" id="Email4" />
          </span></td>
          <td><span class="style1">
            <input type="text" name="Phone4" size="15" id="Phone4" />
          </span></td>
        </tr>
        <tr>
          <td><span class="style1">
            <input type="text" name="Last5" size="15" id="Last5" />
          </span></td>
          <td><span class="style1">
            <input type="text" name="First5" size="15" id="First5" />
          </span></td>
          <td><span class="style1">
            <input type="text" name="Email5" size="15" id="Email5" />
          </span></td>
          <td><span class="style1">
            <input type="text" name="Phone5" size="15" id="Phone5" />
          </span></td>
        </tr>
      </table>
      <p><span class="title">Registrant Agency Information</span>:</p>
      <p align="left" class="style1"><strong>Agency Name*</strong>:
        <input type="text" name="Agency" size="48" id="Agency" />
      </p>
      <p align="left" class="style1">Mailing Address:
        <input type="text" name="Mailing" size="48" id="Mailing" />
      </p>
      <p align="left" class="style1">City:
        <input type="text" name="City" size="48" id="City" /> 
        Zip: 
        <input type="text" name="Zip" size="15" id="Zip" />
      </p>
      <p align="left" class="style1"><strong>Phone*</strong>:
        <input name="AgencyPhone" type="text" id="AgencyPhone" size="48" />
      </p>
       <p align="center"><input type="submit" value="Submit" /></p>
      </form>

I also had a set of radio buttons (named Rate) and check boxes (named Fee) that I deleted out of this. One problem at a time...

Upvotes: 1

Views: 220

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

NEW VERSION (edit)

Previous form processor is located under this one.

There were a few errors.

It was in fact a misplaced semi-colon in Agency Phone: ".$AgencyPhone.";" which should have read as Agency Phone: ".$AgencyPhone.""; and should have been on the outside instead of being "sandwiched", as it were. The OP had it as ";" when it needed to be "";

Another error was that the email variable was not defined. Since the form did not contain an input field called "email", replaced if(isset($_POST['email'])) { with if(isset($_POST['submit'])) {

A foreach was also added to eleviate the work to define each of the form's fields/variables.

<?php

if(isset($_POST['submit'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "Form Submitted";
$email_from = "[email protected]";

function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

// validation expected data exists
if(!isset($_POST['Agency']) ||
    !isset($_POST['AgencyPhone'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');      
}

$Agency = $_POST['Agency']; // required
$AgencyPhone = $_POST['AgencyPhone']; // required
$contact = $_POST['contact']; // not required

$Zip = $_POST['Zip']; // not required

$error_message = "";
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$Agency)) {
$error_message .= 'The Agency Name you entered does not appear to be valid.<br />';
}
if(strlen($AgencyPhone) < 2) {
$error_message .= 'The Agency Phone you entered does not appear to be valid.<br />';
}

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

// This is where all the MAGIC happens
foreach ($_POST as $key=>$value) {
    $email_message.=$key.": " . $value . "\n";
}

$headers  = 'MIME-Version: 1.0' . "\r\n";

$headers .= 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email_from . "\r\n" . 'X-Mailer: PHP/' . phpversion();

if(mail($email_to, $email_subject, $email_message, $headers))
{
  echo "Mail Sent Successfully";


}else{
  echo "Mail Not Sent";
}

?>

<?php 
}
?>


OLD VERSION

Give this a try:

<?php
    // You may have to use "email" or "email_from" - comment one out.
    // if(isset($_POST['email'])) {
    if(isset($_POST['email_from'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "Form Submitted";
$email_from = "[email protected]";

function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

// validation expected data exists
if(!isset($_POST['Agency']) ||
    !isset($_POST['AgencyPhone'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');      
}

$Agency = $_POST['Agency']; // required
$AgencyPhone = $_POST['AgencyPhone']; // required
$contact = $_POST['contact']; // not required

$Zip = $_POST['Zip']; // not required

$error_message = "";
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$Agency)) {
$error_message .= 'The Agency Name you entered does not appear to be valid.<br />';
}
if(strlen($AgencyPhone) < 2) {
$error_message .= 'The Agency Phone you entered does not appear to be valid.<br />';
}
// $email_message = "You received a message from your site!\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message="
Class: ".$contact."
Dates: ".$Dates." through ".$through."
Location: ".$Location."
Fee: ".$Fee.", ".$Rate."

Students:
1: ".$Last1.", ".$First1.", ".$Email1.", ".$Phone1."\n;
2: ".$Last2.", ".$First2.", ".$Email2.", ".$Phone2."\n;
3: ".$Last3.", ".$First3.", ".$Email3.", ".$Phone3."\n;
4: ".$Last4.", ".$First4.", ".$Email4.", ".$Phone4."\n;
5: ".$Last5.", ".$First5.", ".$Email5.", ".$Phone5."\n;

Agency: ".$Agency.";
Mailing: ".$Mailing.";
City: ".$City.";
Zip: ".$Zip.";
Agency Phone: ".$AgencyPhone."
";

// create email headers
// $headers = 'From: $email_from' ."\r\n" . 'Reply-To: $email_from' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

$headers  = 'MIME-Version: 1.0' . "\r\n";

// You can comment out the line below if sending an HTML based message
//$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$headers .= 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email_from . "\r\n" . 'X-Mailer: PHP/' . phpversion();

// Previous mail function with @ symbol
// @mail($email_to, $email_subject, $email_message, $headers); 
  // mail($email_to, $email_subject, $email_message, $headers); 

if(mail($email_to, $email_subject, $email_message, $headers))
{
echo "Mail Sent Successfully";

}else{
echo "Mail Not Sent";
}

?>



<?php 
}    
?>

Upvotes: 1

Related Questions