Php 不使用$\u GET在表单上显示错误

Php 不使用$\u GET在表单上显示错误,php,forms,post,get,Php,Forms,Post,Get,我用PHP创建了一个注册表。提交表单时,我会检查是否有错误(是否使用了姓名或电子邮件,两个密码是否正确)。如果出现错误,我将使用header函数和信息(错误和字段)将用户返回表单,然后使用$\u GET方法显示错误并重新填写表单 是否有一种不使用标题和$\u GET的方法来显示表单上的错误?我可以从$\u POST接收错误信息并重新填写表单吗 我不喜欢使用JavaScript,但如果需要,我会这样做 我的代码工作得很好,只是想知道是否有办法不使用URL 我的登记表: session_start(

我用PHP创建了一个注册表。提交表单时,我会检查是否有错误(是否使用了姓名或电子邮件,两个密码是否正确)。如果出现错误,我将使用
header
函数和信息(错误和字段)将用户返回表单,然后使用
$\u GET
方法显示错误并重新填写表单

是否有一种不使用
标题
$\u GET
的方法来显示表单上的错误?我可以从
$\u POST
接收错误信息并重新填写表单吗

我不喜欢使用JavaScript,但如果需要,我会这样做

我的代码工作得很好,只是想知道是否有办法不使用URL

我的登记表:

session_start();

// The values that are used to display the form after validation has failed.
// Notice that we actually set them below using the flash data if it's available.
$firstName = '';
$lastName = '';

// Do we have any flash data to deal with?
if (isset($_SESSION['flash'])) {

    // Here, we deal with any _errors_
    if (isset($_SESSION['flash']['errors'])): ?>

        <ul>
            <?php foreach ($_SESSION['flash']['errors'] as $field => $error): ?>
                <li><?php echo $error; ?></li>
            <?php endforeach; ?>
        </ul>
    <?php endif;

    // Here we deal with populating the form again from _data_
    if (isset($_SESSION['flash']['data'])) {
        $firstName = $_SESSION['flash']['data']['first_name'] ?: '';
        $lastName = $_SESSION['flash']['data']['last_name'] ?: '';
    }

    // Remove the flash data from the session since we only want it around for a single request
    unset($_SESSION['flash']);
}
?>
<form method="post" action="handler.php">

    <label>
        <input type="text" name="first_name" placeholder="First Name" value="<?php echo $firstName; ?>">
    </label>

    <label>
        <input type="text" name="last_name" placeholder="Last Name" value="<?php echo $lastName; ?>">
    </label>

    <input type="submit" name="submit">
</form>
session_start();

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

    $firstName = $_POST['first_name'] ?: null;
    $lastName = $_POST['last_name'] ?: null;

    $errors = [];

    if (empty($firstName)) {
        $errors['first_name'] = 'Please enter your first name';
    }

    if (empty($lastName)) {
        $errors['last_name'] = 'Please enter your last name';
    }

    // If we have errors, set up our flash data so it is accessible on the next request and then go back to the form. 
    if ($errors) {
        $_SESSION['flash']['errors'] = $errors;
        $_SESSION['flash']['data'] = $_POST;

        header('Location: index.php');
        exit;
    }

    // We know there are no errors at this point so continue processing...

}

处理错误和注册的php文件:

session_start();

// The values that are used to display the form after validation has failed.
// Notice that we actually set them below using the flash data if it's available.
$firstName = '';
$lastName = '';

// Do we have any flash data to deal with?
if (isset($_SESSION['flash'])) {

    // Here, we deal with any _errors_
    if (isset($_SESSION['flash']['errors'])): ?>

        <ul>
            <?php foreach ($_SESSION['flash']['errors'] as $field => $error): ?>
                <li><?php echo $error; ?></li>
            <?php endforeach; ?>
        </ul>
    <?php endif;

    // Here we deal with populating the form again from _data_
    if (isset($_SESSION['flash']['data'])) {
        $firstName = $_SESSION['flash']['data']['first_name'] ?: '';
        $lastName = $_SESSION['flash']['data']['last_name'] ?: '';
    }

    // Remove the flash data from the session since we only want it around for a single request
    unset($_SESSION['flash']);
}
?>
<form method="post" action="handler.php">

    <label>
        <input type="text" name="first_name" placeholder="First Name" value="<?php echo $firstName; ?>">
    </label>

    <label>
        <input type="text" name="last_name" placeholder="Last Name" value="<?php echo $lastName; ?>">
    </label>

    <input type="submit" name="submit">
</form>
session_start();

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

    $firstName = $_POST['first_name'] ?: null;
    $lastName = $_POST['last_name'] ?: null;

    $errors = [];

    if (empty($firstName)) {
        $errors['first_name'] = 'Please enter your first name';
    }

    if (empty($lastName)) {
        $errors['last_name'] = 'Please enter your last name';
    }

    // If we have errors, set up our flash data so it is accessible on the next request and then go back to the form. 
    if ($errors) {
        $_SESSION['flash']['errors'] = $errors;
        $_SESSION['flash']['data'] = $_POST;

        header('Location: index.php');
        exit;
    }

    // We know there are no errors at this point so continue processing...

}
“回发”是指发件人提交到自身(同一页)的情况

使用这种方法,当发现错误时,您不需要一堆重定向

页面顶部有PHP,如下所示:

$error_text = "";
if (isset($_POST['submit_button'])) {
    ... validate all data ....
    ... adding to $error_text for any errors found ...
    if ($is_valid)
        ... process form ...
        ... display results page & exit()
    else
        ... fall through to displaying form page below
} // end of form submit handling

// if we reach here, either there was no form submit (first time page displayed)
// or the form was submitted but errors were found and $error_txt is now something like 
// <p>Error: passwords must match</p>
?>

<html>
...
<?php echo $error_txt; ?>
<form>
...
$error_text=”“;
如果(isset($_POST['submit_button'])){
…验证所有数据。。。。
…正在将发现的任何错误添加到$error\u文本中。。。
如果($有效)
…处理表单。。。
…显示结果页&退出()
其他的
…转到下面的显示表单页
}//表单结束提交处理
//如果我们到达这里,要么没有表单提交(第一次显示页面)
//或者表单已提交,但发现了错误,$error_txt现在类似于
//错误:密码必须匹配

?> ... ...
当发件人提交到自己(同一页)时,即为“回发”

使用这种方法,当发现错误时,您不需要一堆重定向

页面顶部有PHP,如下所示:

$error_text = "";
if (isset($_POST['submit_button'])) {
    ... validate all data ....
    ... adding to $error_text for any errors found ...
    if ($is_valid)
        ... process form ...
        ... display results page & exit()
    else
        ... fall through to displaying form page below
} // end of form submit handling

// if we reach here, either there was no form submit (first time page displayed)
// or the form was submitted but errors were found and $error_txt is now something like 
// <p>Error: passwords must match</p>
?>

<html>
...
<?php echo $error_txt; ?>
<form>
...
$error_text=”“;
如果(isset($_POST['submit_button'])){
…验证所有数据。。。。
…正在将发现的任何错误添加到$error\u文本中。。。
如果($有效)
…处理表单。。。
…显示结果页&退出()
其他的
…转到下面的显示表单页
}//表单结束提交处理
//如果我们到达这里,要么没有表单提交(第一次显示页面)
//或者表单已提交,但发现了错误,$error_txt现在类似于
//错误:密码必须匹配

?> ... ...
如果要使用单独的脚本来处理表单处理,可以使用会话来保存临时数据,这通常称为闪存数据

在下面的示例中,我们将错误和数据设置到会话中,以便可以从index.php访问它

处理完闪存数据后,我们将其从会话中删除,因为我们不希望在下一个请求中看到它

index.php
session_start();

// The values that are used to display the form after validation has failed.
// Notice that we actually set them below using the flash data if it's available.
$firstName = '';
$lastName = '';

// Do we have any flash data to deal with?
if (isset($_SESSION['flash'])) {

    // Here, we deal with any _errors_
    if (isset($_SESSION['flash']['errors'])): ?>

        <ul>
            <?php foreach ($_SESSION['flash']['errors'] as $field => $error): ?>
                <li><?php echo $error; ?></li>
            <?php endforeach; ?>
        </ul>
    <?php endif;

    // Here we deal with populating the form again from _data_
    if (isset($_SESSION['flash']['data'])) {
        $firstName = $_SESSION['flash']['data']['first_name'] ?: '';
        $lastName = $_SESSION['flash']['data']['last_name'] ?: '';
    }

    // Remove the flash data from the session since we only want it around for a single request
    unset($_SESSION['flash']);
}
?>
<form method="post" action="handler.php">

    <label>
        <input type="text" name="first_name" placeholder="First Name" value="<?php echo $firstName; ?>">
    </label>

    <label>
        <input type="text" name="last_name" placeholder="Last Name" value="<?php echo $lastName; ?>">
    </label>

    <input type="submit" name="submit">
</form>
session_start();

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

    $firstName = $_POST['first_name'] ?: null;
    $lastName = $_POST['last_name'] ?: null;

    $errors = [];

    if (empty($firstName)) {
        $errors['first_name'] = 'Please enter your first name';
    }

    if (empty($lastName)) {
        $errors['last_name'] = 'Please enter your last name';
    }

    // If we have errors, set up our flash data so it is accessible on the next request and then go back to the form. 
    if ($errors) {
        $_SESSION['flash']['errors'] = $errors;
        $_SESSION['flash']['data'] = $_POST;

        header('Location: index.php');
        exit;
    }

    // We know there are no errors at this point so continue processing...

}

如果要使用单独的脚本来处理表单处理,可以使用会话来保存临时数据,这通常称为闪存数据

在下面的示例中,我们将错误和数据设置到会话中,以便可以从index.php访问它

处理完闪存数据后,我们将其从会话中删除,因为我们不希望在下一个请求中看到它

index.php
session_start();

// The values that are used to display the form after validation has failed.
// Notice that we actually set them below using the flash data if it's available.
$firstName = '';
$lastName = '';

// Do we have any flash data to deal with?
if (isset($_SESSION['flash'])) {

    // Here, we deal with any _errors_
    if (isset($_SESSION['flash']['errors'])): ?>

        <ul>
            <?php foreach ($_SESSION['flash']['errors'] as $field => $error): ?>
                <li><?php echo $error; ?></li>
            <?php endforeach; ?>
        </ul>
    <?php endif;

    // Here we deal with populating the form again from _data_
    if (isset($_SESSION['flash']['data'])) {
        $firstName = $_SESSION['flash']['data']['first_name'] ?: '';
        $lastName = $_SESSION['flash']['data']['last_name'] ?: '';
    }

    // Remove the flash data from the session since we only want it around for a single request
    unset($_SESSION['flash']);
}
?>
<form method="post" action="handler.php">

    <label>
        <input type="text" name="first_name" placeholder="First Name" value="<?php echo $firstName; ?>">
    </label>

    <label>
        <input type="text" name="last_name" placeholder="Last Name" value="<?php echo $lastName; ?>">
    </label>

    <input type="submit" name="submit">
</form>
session_start();

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

    $firstName = $_POST['first_name'] ?: null;
    $lastName = $_POST['last_name'] ?: null;

    $errors = [];

    if (empty($firstName)) {
        $errors['first_name'] = 'Please enter your first name';
    }

    if (empty($lastName)) {
        $errors['last_name'] = 'Please enter your last name';
    }

    // If we have errors, set up our flash data so it is accessible on the next request and then go back to the form. 
    if ($errors) {
        $_SESSION['flash']['errors'] = $errors;
        $_SESSION['flash']['data'] = $_POST;

        header('Location: index.php');
        exit;
    }

    // We know there are no errors at this point so continue processing...

}

请分享一些代码,以便我们了解您是如何解决问题的。您可以使用会话临时存储信息。添加我的代码,如果(空($name)或空($email)或空($pwd)或空($pwd2)){//echo'请填写表单';}……请,共享一些代码,以便我们了解您是如何解决问题的您可以使用会话临时存储信息。添加我的代码,如果(空($name)或空($email)或空($pwd)或空($pwd2)){//echo'请填写表单';}……向上投票。戴夫,我的回答完全一样。他还可以检查会话以避免重复提交。我希望避免页面本身的表单验证。这正是我编写代码的方式,因此您必须使用会话来跟踪错误并重新填充表单。查看flash消息。上传。戴夫,我的回答完全一样。他还可以检查会话以避免重复提交。我希望避免页面本身的表单验证。这正是我编写代码的方式,因此您必须使用会话来跟踪错误并重新填充表单。查看flash消息。