单独的表单,按特定顺序执行2个操作


Separate forms, 2 actions in specific order

我有一个表单要求用户输入。输入应该是一个存在于我的数据库中的名称。

<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    Name: <input type="text" name="name" value="<?php echo $name;?>">
    <span class="error">* <?php echo $nameErr;?></span>
    <br><br>
    <input type="submit" name="login" value="Login">
</form>

我试图首先检查用户输入,然后如果输入是干净的,将用户发送到另一个网站。

这是我用来清除输入的:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $nameErr = "Name is required";
    } else {
        $name = test_input($_POST["name"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
            $nameErr = "Only letters and white space allowed"; 
        }
    }
}

然后我想在查询我的数据库并收到ID:后发送到下一个网站

<form action = "http://website.com/~username/site.php" method="get">
    Name: <input type="text" name="name"><br>
    <Input type="submit">
</form>

我该怎么做?

使用header()函数实现目标

header('Location: yourfile.php?id=' . $yourIdValue); // adopt to your needs

在您的目标脚本中使用获取id

$id = $_GET['id'];