在php的if语句中使用if语句重定向到一个网页,覆盖meta html


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

如果满足条件,我想重定向到网页。

如果不满足条件,我已经使用meta来重定向了。

<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>");
}
?>

我尝试使用$forward,但它不起作用。不使用Meta或$forward重定向的其他方法吗?

谢谢

检查这是否符合您发布的代码。我只做了一些修改。检查是否所有条件都如您所愿

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";
}

After:

header ("Location:$location");

您需要添加exit();

因此,代码将是:
header ("Location:$location");
exit();