有没有办法从check.php获取POST数据到index.php&;谢谢.php?

有没有办法从check.php获取POST数据到index.php&;谢谢.php?,php,post,Php,Post,我的目标是使我的表单在我的index.php上的每个输入文本表单的下方显示错误消息,如果有空白,如下图所示。我不想使用任何像Laravel或Cakephp这样的框架,只想使用POST方法。 目前,我没有收到任何消息,但在出现空白时重定向到index.php 下面是我的input.php代码 <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta

我的目标是使我的表单在我的index.php上的每个输入文本表单的下方显示错误消息,如果有空白,如下图所示。我不想使用任何像Laravel或Cakephp这样的框架,只想使用POST方法。

目前,我没有收到任何消息,但在出现空白时重定向到index.php

下面是我的input.php代码

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Practice1</title>
</head>
<body>
    <form action="check.php" method="post">
    <h1>Input form</h1>
    <div class="content">
    Last Name&nbsp;&nbsp;<input type="text" name="last-name" value=""class="">
    <?php if(isset($errors['lastName'])) : ?>
        <p><?php echo htmlspecialchars("") ?></p>
    <?php endif; ?>
    </div>

    <div class="content">
    Name&nbsp;&nbsp;<input type="text" name="first-name" value="">
    <?php if (isset($errors['firstName'])): ?>
    <p><?php echo $errors['firstName'] ?></p>
    <?php endif; ?>
    </div>

  </form>
</body>
</html>

练习1
输入表格
姓

名称

这是我的check.php代码

<?php
if($_SERVER["REQUEST_METHOD"] != "POST"){
    header("location: index.php");
    exit;
}
$firstName = $_POST['first-name'];
$lastName = $_POST['last-name'];

if ($lastName == "" || $firstName == ""){
    header("location: index.php");
    exit;
}

$errors = ['lastName' => '※required', 'firstName' => '※required'];

?>

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>confirm</title>
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
<form action="thanks.php index.php" method="post">
    <h1>confirm</h1>
    <div class="content">
    <p><?php echo htmlspecialchars($lastName) ?></p>
    </div>
    <div class="content">
    <p><?php echo htmlspecialchars($firstName) ?></p>
    </div>

确认
确认


您正在发出标题重定向并退出,因此下面设置错误的行永远不会被调用。。。不要尝试删除退出行,它将执行相同的操作,因为浏览器在接收到标题位置时会立即“处理”标题位置。您必须正确编码错误结构,以便将其作为GET参数传递给重定向脚本(如果您希望这样做),例如,使用url_encode,您还可以考虑使用Ajax提交表单。然后你不需要重新加载页面或向URL添加内容就可以显示错误消息。我已经尝试在重定向结构上方对错误结构进行编码,但结果仍然是一样的。谢谢你的回答,但我不喜欢使用其他语言而不是PHP和HTML。