0

I want to have a redirection (to index.php, my homepage) when the message in my contact form has been sent but I don't see how to do it so I ask your precious help.

Here is my PHP code:

<?php
$to       = "test@test.me"; 
$name     = $_REQUEST["name"];
$email    = $_REQUEST["email"];
$subject  = $_REQUEST["subject"];
$msg      = $_REQUEST["message"];

if (isset($email) && isset($name)) {
     
  $website = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; 
  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  $headers .= "From: ".$name." <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
  $msg     = "Bonjour Anne-Valérie, <br/> <br/> vous avez un nouveau message de $name ($email)<br/><br/>Message: $msg <br><br> -- <br>Cet email a été envoyé depuis $website";
    
  $mail =  mail($to, $subject, $msg, $headers);
  if($mail)
  {
    echo 'Votre mail a bien été envoyé, une réponse vous sera apporté sous peu, merci de votre confiance.';
  }

  else
  {
    echo "Oups, quelque chose n'a pas fonctionné, veuillez vérifier vos informations.";
  }
}
?>
Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
  • You can use a call to `header("Location:./index.php"); exit();`. However, you **cannot** echo anything before this call. So you have to remove your `echo` rows before... or manage it with other methods – Gowire Jan 22 '21 at 10:13
  • [Is this answer your question](https://stackoverflow.com/questions/768431/how-do-i-make-a-redirect-in-php) – emppeak Jan 22 '21 at 10:13
  • Thank you for your time Gowire. Where do I have to put echo then please ? – Quentin Duprat Jan 22 '21 at 10:17

1 Answers1

0

If you want to redirect use header("Location: http://example.com/myOtherPage.php");. The question has already been answered How do I make a redirect in PHP? check it out .