开发者

PHP Mailer: help with echo if form submitted correctly

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

Where do I place this alert into the following code if I want i开发者_JAVA百科t to appear if the form is submitted correctly?

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

Thanks


Here is my best attempt at making some sense of the above code. There are several things I do not understand, notably exactly where in the document flow this script occurs - have you already output some HTML at this stage? Also, there is a line the reads simply $url; and I cannot figure out what it is supposed to do. What it definitely does do is nothing.

<?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");

?>


The mail function returns a boolean value that indicates if your mail was sent or not.

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

For a further reference, take a look at php documentation:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜