表单完成后PHP重定向

时间:2012-03-26 10:09:28

标签: php email redirect

<?php

// Define some constants
define( "RECIPIENT_NAME", "John Smith" );
define( "RECIPIENT_EMAIL", "john@example.com" );
define( "EMAIL_SUBJECT", "Visitor Message" );

// Read the form values
$success = false;
$senderName = isset( $_POST['senderName'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['senderName'] ) : "";
$senderEmail = isset( $_POST['senderEmail'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderEmail'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";

// If all values exist, send the email
if ( $senderName && $senderEmail && $message ) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = "From: " . $senderName . " <" . $senderEmail . ">";
  $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
}

// Return an appropriate response to the browser
if ( isset($_GET["ajax"]) ) {
  echo $success ? "success" : "error";
} else {
?>
<html>
  <head>
    <title>Thanks!</title>
  </head>
  <body>
  <?php if ( $success ) echo "<p>Thanks for sending your message! We'll get back to you shortly.</p>" ?>
  <?php if ( !$success ) echo "<p>There was a problem sending your message. Please try again.</p>" ?>
  <p>Click your browser's Back button to return to the page.</p>
  </body>
</html>
<?php
}
?>

好的,这是我用来发送邮件的代码。 我有使用这个php脚本的contact.html和表单。 如果成功,我希望此脚本转到或重定向到某个页面(例如contact_success.html),如果出现错误,则转到另一个页面(contact_error.html)。 感谢

5 个答案:

答案 0 :(得分:6)

if ($success) {
 header("Location: /success.html");
 exit;
} else {
 header("Location: /error.html");
 exit;
}

答案 1 :(得分:0)

if($success) {
    header('location:contact_success.html');
}
else {
    header('location:contact_error.html');
}

答案 2 :(得分:0)

试试这段代码

if ($success == 'success') {
 header("Location: success.html");
 exit;
} else {
 header("Location: error.html");
 exit;
}

答案 3 :(得分:0)

这是我认为你想要做的非常基本的信息。在提交表单后,它会重定向到网址。

<?php
$goto_after_mail = "http://www.domain.com"; // This is the URL that is shown after the mail is submitted.
$from_mail = $_REQUEST['email']; // Be sure your EMAIL form field is identified as "email".
$from_name = $_REQUEST['name']; // Be sure your NAME form field is    identified as "name".
$header = "From: \\"$from_name\\" <$from_mail>\\r\
";
$header .= "MIME-Version: 1.0\\r\
";
$header .= "Content-Type: text/plain; charset=\\"utf-8\\"\\r\
";
$header .= "Content-Transfer-Encoding: 7bit\\r\
";
$subject = "Your Message Subject"; // Insert your message subject.
foreach ($_REQUEST as $key => $val) {
    if ($key != "" && $key != "") {
        $body .= $key . " : " . $val . "\\r\
";
    }
}
if (mail("mail@domain.com", $subject, $body, $header)) { // Insert your  e-mail address to be used to view your submissions.
    header("Location: ".$goto_after_mail);
}
?>

虽然这是基本的,但您应该能够阅读代码并获得想法。

问候,汤姆

答案 4 :(得分:0)

非常好。在检查邮件功能的实际完成情况后,可以使用正确的重定向添加到您的sendmail脚本。

希望这有用!

//Email Notification
  // the message
  $msg = $today." : A NEW Message.\n\Property: ".$property."\Value: ".$Value."\n";

  // use wordwrap() if lines are longer than 70 characters
  // we'll use base 64 if needed with the later email templates
  $msg = wordwrap($msg,70);

  // old send email
  //mail("someemail@gmail.com,info@somedomain.com","some Message",$msg);

// Redirect after the mail is sent so they are not blank
if (mail("someemail@address.com"," - A New item has posted",$msg)){
  //  To redirect form on a particular page
  header("Location: http://google.com");
}