繁体   English   中英

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

[英]PHP Mailer: help with echo if form submitted correctly

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.'">';
?>

谢谢

这是我对上述代码有所了解的最佳尝试。 我有几件我不了解的事情,特别是该脚本在文档流中的确切位置-您是否已在此阶段输出了一些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");

?>

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

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

有关更多参考,请查看php文档:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM