I am having some problems with redirecting to another page in Php.
I am using :
header("Location: mypage.html");
But instead of redirecting to this page it is just printing the html file of the 'mypage.html' into the console. With console being the debug console in the Google Chrome inspect view aka (ctrl + shift + i).
This is the relevant part of the code for this error:
<?php
ob_clean(); // did not work...
function isValid(){
if(
$_POST["name"] != "" &&
$_POST["tel"] != "" &&
$_POST["email"] != "" &&
$_POST["msg"] != "" &&
$_POST["reg"] != ""
) {
return true;
}
return false;
}
if (isValid()){
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify'; // URL to the reCAPTCHA server
$recaptcha_secret = 'XXXXXXsecretkeyXXXXX'; // Secret key
$recaptcha_response = $_POST['recaptchaResponse']; // Response from reCAPTCHA server, added to the form during processing
$recaptcha = file_get_contents($recaptcha_url.'?secret='.$recaptcha_secret.'&response='.$recaptcha_response); // Send request to the server
$recaptcha = json_decode($recaptcha); // Decode the JSON response
if($recaptcha->success == true && $recaptcha->score >= 0.5 && $recaptcha->action == "submit"){
// echo "Score : ".$recaptcha->score;
// Send the email
$Name = $_POST["name"];
$Tel = $_POST["tel"];
$MailFrom = $_POST["email"];
$Message = UTF8_decode($_POST["msg"]);
$Reg = $_POST["reg"];
$MailTo = "mail@mail.com";
$Subject = "".$Name." | Mail@mail.com";
$Headers = array(
'From' => $MailTo,
'Reply-To' => $MailTo,
'MIME-Version' => '1.0',
'Content-type' => 'text/html',
'charset' => 'UTF-8',
'X-Mailer' => 'PHP/' . phpversion()
);
$Msg = 'Namn: ' . $Name . '<br />Telefonnummer: ' . $Tel . '<br />E-mail: ' . $MailFrom . '<br />Regnummer: ' . $Reg . '<br /><br />Meddelande: ' . wordwrap($Message, 50);
$test = mail($MailTo, $Subject, $Msg, $Headers);
if ($test === true) {
ob_clean(); // this did not work here either...
header("Location: redirect.html"); // this is where the error is!
} else {
echo "Meddelandet kunde inte skickas, vänligen kontakta oss på följande mail. (mail@mail.com)";
}
} else {
// reCAPTCHA failed, dont send mail
echo "Vi tror att du är en robot, är du inte en robot så vänligen kontakta oss på följande mail. (mail@mail.com)";
}
}
?>
To fix this problem I have tried to remove all of the echos in the php file since I thought that it might not work with echos. But no success.
I have also tried to use a function called ob_clean() to prevent it from writing in the console but no luck there either.
Any ideas would be greatly appreciated.