PHP表单,测试后如果字段不为空,请转到新的page.php。怎么做?

时间:2016-02-03 21:04:28

标签: php

所以我有bandau.php

<?php
include "bandau1.php";
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    Vardas: <input type="text" name="vardas">
    <span class="error">* <?php echo $vardasErr; ?></span><br>
    Pavarde: <input type="text" name="pavarde">
    <span class="error">* <?php echo $pavardeErr; ?></span><br>
    Lytis: <input type="radio" name="lytis" value="vyras">Vyras
    <input type="radio" name="lytis" value="moteris">Moteris
    <span class="error">* <?php echo $lytisErr; ?></span><br>
    <input type="submit"><br>
</form>

然后我有bandau1.php

<?php

$vardas     = $pavarde    = $lytis      = "";
$vardasErr  = $pavardeErr = $lytisErr   = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{

    if (empty($_POST["vardas"]))
    {
        $vardasErr = "Iveskite varda";
    }
    else
    {
        $vardas = $_POST["vardas"];
    }

    if (empty($_POST["pavarde"]))
    {
        $pavardeErr = "Iveskite pavarde";
    }
    else
    {
        $pavarde = $_POST["pavarde"];
    }

    if (empty($_POST["lytis"]))
    {
        $lytisErr = "Nepasirinkote lyties";
    }
    else
    {
        $lytis = $_POST["lytis"];
    }
}

我的问题是,在推送any.php之后,当我想要转到新文件submit时,如何使脚本生效?如果我改为<form method="post" action="any.php>那么bandau1.php就会停止工作。它不会检查字段是否为空。

我想转到这段代码:

<?php

$x = $_POST["vardas"];
$b = $_POST["pavarde"];
$c = $_POST["lytis"];
echo "Jusu atsakymai priskirti siai lyciu kategorijai " . $c;
print "\r\n";
echo " Jusu atsakymai uzfiksuoti siuo vardu: " . $x . " " . $b;

1 个答案:

答案 0 :(得分:0)

您可以在处理表单后使用header()进行重定向。

if($_SERVER["REQUEST_METHOD"] == "POST"){
    // All your existing code..
    header("Location: any.php");
}

或者如果你仍需要在any.php上使用POST变量,你可以这样做。

if($_SERVER["REQUEST_METHOD"] == "POST"){
    // All your existing code..
    require("Location: any.php");
    exit;
}