PHP Mailer: помогите с echo, если форма отправлена ​​правильно - PullRequest
0 голосов
/ 13 сентября 2011
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 ]

1 голос
/ 13 сентября 2011

Вот моя лучшая попытка разобраться в приведенном выше коде.Есть несколько вещей, которые я не понимаю, в частности, где именно в потоке документов находится этот скрипт - вы уже вывели какой-то 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");

?>
0 голосов
/ 13 сентября 2011

Функция mail возвращает логическое значение, которое указывает, было ли отправлено ваше письмо.

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

Для дальнейшего ознакомления посмотрите документацию php:

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

...