繁体   English   中英

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

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

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

如果不满足条件,我已经在使用元进行重定向。

<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 重定向的其他方法是什么?

谢谢

后:

header ("Location:$location");

您需要添加exit();

所以,代码将是:

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

根据您发布的代码检查这是否有效。 我只做了一些编辑。 检查所有条件是否也符合您的预期

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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