PHP перенаправление после заполнения формы - PullRequest
1 голос
/ 26 марта 2012
<?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
}
?>

Хорошо, это код, который я использую для отправки почты. У меня есть contact.html и форма, которая использует этот скрипт php. Я хотел бы, чтобы этот скрипт, чтобы перейти или перенаправить на какую-то страницу (например, contact_success.html) в случае успеха или перейти на другую страницу в случае ошибки (contact_error.html). Спасибо

Ответы [ 5 ]

6 голосов
/ 26 марта 2012
if ($success) {
 header("Location: /success.html");
 exit;
} else {
 header("Location: /error.html");
 exit;
}
0 голосов
/ 24 июля 2017

Очень мило. Вот что нужно добавить в ваш скрипт sendmail с правильным перенаправлением после проверки фактического завершения функции mail.

Надеюсь, это полезно!

//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");
}
0 голосов
/ 04 ноября 2016

Это очень базовая информация о том, что, я думаю, вы пытаетесь сделать.После отправки формы он перенаправляется на URL-адрес.

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

Несмотря на то, что это основа, вы должны быть в состоянии прочитать код и получить представление.

С уважением, Том

0 голосов
/ 26 марта 2012

попробуйте этот код

if ($success == 'success') {
 header("Location: success.html");
 exit;
} else {
 header("Location: error.html");
 exit;
}
0 голосов
/ 26 марта 2012
if($success) {
    header('location:contact_success.html');
}
else {
    header('location:contact_error.html');
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...