1

我已经多次从这个网站获得帮助,但这是我第一次需要发布我自己的细节。

我有一个 PHP 注册表单,我确信这是一个小问题,但我不知道需要修复什么。所以,我很感激有人看过它。

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

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "c@d.com";
$email_subject = "Form Submitted";
$email_from = "c@d.com";

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 
}    
?>

所以,错误就在这一行。

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

或者,无论如何,一个错误是。缺少什么,或者我需要添加什么?感谢您提供的任何帮助。对此,我真的非常感激。

我的表格是:

<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>

我还删除了一组单选按钮(名为 Rate)和复选框(名为 Fee)。一次一个问题...

4

1 回答 1

1

新版本(编辑)

以前的表单处理器位于这个下方。

有一些错误。

实际上,它是一个错放的分号Agency Phone: ".$AgencyPhone.";",应该读作Agency Phone: ".$AgencyPhone."";并且应该在外面,而不是像原来的那样被“夹在中间”。OP 在";"需要时拥有它"";

另一个错误是email未定义变量。由于表单不包含名为“电子邮件”的输入字段,因此替换if(isset($_POST['email'])) {if(isset($_POST['submit'])) {

还添加了Aforeach以提升定义每个表单字段/变量的工作。

<?php

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

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "c@d.com";
$email_subject = "Form Submitted";
$email_from = "c@d.com";

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 
}
?>


旧版

试试这个:

<?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 = "c@d.com";
$email_subject = "Form Submitted";
$email_from = "c@d.com";

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 
}    
?>
于 2013-08-07T20:43:08.200 回答