Plavo Oko
Plavo Oko

Reputation: 11

PHP redirect after form completed

<?php

// Define some constants
define( "RECIPIENT_NAME", "John Smith" );
define( "RECIPIENT_EMAIL", "[email protected]" );
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
}
?>

Ok, so this is the code that i am using to send mail. I have contact.html and form that uses this php script. I would like this script to go to or redirect to some page (eg. contact_success.html) if success or go to another page if error (contact_error.html). Thanks

Upvotes: 1

Views: 13345

Answers (5)

LinuxHombre
LinuxHombre

Reputation: 1

Very nice. Here's to add to your sendmail script with proper redirection after checking the actual completion of the mail function.

Hope this is helpful!

//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("[email protected],[email protected]","some Message",$msg);

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

Upvotes: 0

tashworth19191
tashworth19191

Reputation: 27

This is very basic information on what I think you are trying to do. It redirects to a url after form is submitted.

<?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("[email protected]", $subject, $body, $header)) { // Insert your  e-mail address to be used to view your submissions.
    header("Location: ".$goto_after_mail);
}
?>

Although this is basic, you should be able to read the code and get the idea.

Regards, Tom

Upvotes: 0

yashmathur
yashmathur

Reputation: 65

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

Upvotes: 0

Naveen Kumar
Naveen Kumar

Reputation: 4601

try this code

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

Upvotes: 0

Not_a_Golfer
Not_a_Golfer

Reputation: 49265

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

Upvotes: 6

Related Questions