简体   繁体   中英

Redirect to a webpage using an if statement within an if statement in php, override meta html

I want to redirect to a webpage if a condition is met.

I'm already using meta to redirect if the condition is not met.

<meta http-equiv="refresh" content="4; url=form3.html" />

<?php

$to = "example@example.at"; 
$subject = "School Info";
$headers = "From: Free Project Day"; 
$field_school = $_POST['school'];
$field_email = $_POST['email'];
$date = date('d/m/Y');
$forward = "1";
$forward2 ="0";
$location = "index.html";


if (empty($field_school) || empty($field_email) && empty($field_tel) ) {
    echo 'Please correct the fields';
    return false;
    if ($forward == 1) {
        header ("Location:$location");
    }
}

$msg = "TEXT $date.\n\n";

foreach ($_POST as $key => $value) {
    $msg .= ucfirst ($key) ." : ". $value . "\n";
}    

mail($to, $subject, $msg, $headers);
if ($forward2 == 1) {
    header ("Location:$location");
}
else {
    echo ("TEXT>");
}
?>

I tried to use the $forward but it did not work. Are the other ways to redirect without using Meta or $forward?

Thanks

After:

header ("Location:$location");

You need to add exit();

So, the code will be:

header ("Location:$location");
exit();

Check if this works according your posted codes. I made just few edits. Check if all conditions are as you intend as well

if ((empty($field_school) || empty($field_email)) && empty($field_tel) ) 
{
    echo 'Please correct the fields';
    //return false; No need for this as there's not function here
    if ($forward == 1) 
    {
        header('refresh:4;url='.$location);
        exit();
    }
}

$msg = "TEXT $date.\n\n";

foreach ($_POST as $key => $value) 
{
    $msg .= ucfirst ($key) ." : ". $value . "\n";
}    

mail($to, $subject, $msg, $headers);
if ($forward2 == 1) 
{
        header('refresh:4;url='.$location);
        exit();
}
else {
    echo "your messages here";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM