PHP Mailer:如果表单正确提交,则帮助回显

时间:2011-09-13 08:33:39

标签: php javascript echo mailer

echo '<script> alert("THANK YOU FOR SUBSCRIBING TO THE NEWSLETTER") </script>';

如果我希望在正确提交表单时出现此警告,我该将警报放在以下代码中?

<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Add this Email Address to Mailing List'; 

// Your email address. This is where the form information will be sent. 
$emailadd = 'test398ty32@gmail.com'; 

// Where to redirect after form is processed. 
$url = 'http://10.0.1.1/~macpro';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '1'; 

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n"; 
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo '<script> alert("PLEASE ENTER A VALID EMAIL ADDRESS") </script>';$url; }
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

由于

2 个答案:

答案 0 :(得分:1)

这是我对上述代码有所了解的最佳尝试。有几点我不明白,特别是文档流程中这个脚本出现的确切位置 - 您是否已经在此阶段输出了一些HTML?此外,有一行只读$url;,我无法弄清楚它应该做什么。它绝对没有做的事情。

<?php

  //--------------------------Set these parameters--------------------------

  // Subject of email sent to you.
  $subject = 'Add this Email Address to Mailing List'; 

  // Your email address. This is where the form information will be sent. 
  $emailadd = 'test398ty32@gmail.com'; 

  // Where to redirect after form is processed. 
  $url = 'http://10.0.1.1/~macpro';

  // Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
  $req = '1'; 

  // --------------------------Do not edit below this line--------------------------

  $text = "Results from form:\n\n"; 
  foreach ($_POST as $key => $value) {
    if ($req == '1' && trim($value) == '') { // This is not a great validation check - what if the user enters some nonsense value?
      // Is every field an email address? Which field are we dealing with here?
      echo '<script type="text/javascript"> alert("Please enter a valid email address"); </script>';
      // I don't get what this is supposed to do
      $url;
    }
    // This sort of makes sense, but if you are using a meta refresh it implies we are
    // still in the document head, so shouldn't you end it before outputing text?
    if (strlen($key) >= 20) die("Name of form element $key cannot be longer than 20 characters");
    // All of that spacing nonsense can be compressed to this one line
    $text .= str_pad("$key:", 22, ' ', STR_PAD_RIGHT)."$value\n";
  }

  // Sends the email - this works for now although you should consider using a library
  // like phpmailer or PEAR:Mail
  if (mail($emailadd, $subject, $text, "From: $emailadd")) {
    // The email was sent successfully
    echo '<script type="text/javascript"> alert("Thankyou for subscribing to the newsletter"); </script>';
  } else {
    // Sending the email failed
    echo '<script type="text/javascript"> alert("Failed to send email"); </script>';
  }

  // Refreshes the page - META refreshes are bad practice, you should really try to
  // restructure all this code to allow you to do a header redirect (see below)
  echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
  // Work towards being able to do this instead
  // header('HTTP/1.1 303 See Other');
  // header("Location: $url");

?>

答案 1 :(得分:0)

mail函数返回一个布尔值,指示您的邮件是否已发送。

$mail = mail($to, $subject, $message, $headers);
if($mail){
    //your code here
}

有关进一步的参考,请查看php文档:

http://php.net/manual/en/function.mail.php