Pavel Nedev
Pavel Nedev

Reputation: 3

PHP redirect on form submission not working

I am having trouble with a basic contact form and everything I've read, doesn't seem to help, I tried different approaches, but nothing really worked. The idea is after the user sends his message, he gets redirected to a success/error page. Everything seems to work, the form works - I get emails from it, but it doesn't redirect me to the requested page. Any advice would be much appreciated!

Here are the codes for the form.

    <div class="text-intro">

      <h1>Contact</h1><br><br>
      <p>To contact us please use the contact form visible. When sending files, please use the following e-mail</p><br><br><br><br><br>

      <form method="post" action="contactengine.php">

      <div class="contact-one">
        <p>
          <label for="Name">Username:</label><br><br>
          <input name="Name" id="Name" data-jkit="[validate:required=true;min=3;max=10;error=Please enter your username (3-10 characters)]">
        </p>
      </div>  

      <div class="contact-two">  
        <p>
          <label for="Email">E-mail:</label><br><br>
          <input name="Email" id="Email" data-jkit="[validate:required=true;strength=50;error=Please write your email]">
        </p>
      </div>  

      <div class="contact-three">  
        <p>
          <label for="Message">Message:</label><br><br>
          <textarea id="Message" name="Message"></textarea><br><br>

                  <input type="submit" name="submit" value="Submit" class="submit-button">
        </p>
      </div>            

        <p>
        </p>
      </form>


    </div>   

PHP:

<?php

$EmailFrom = "[email protected]";
$EmailTo = "[email protected]";
$Subject = "New message";
$Name = Trim(stripslashes($_POST['Name'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 



// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");


// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}
?>

Upvotes: 0

Views: 112

Answers (1)

Illedhar
Illedhar

Reputation: 251

What I would recommend to you, instead of printing the HTML, is to modify the header in order to redirect the browser. This is what I always used when redirecting. It works as follows:

header('Location: http://www.example.com/');
exit;

Be sure that there is no output before sending the header.

Upvotes: 3

Related Questions