如何在 php 中使用 $_GET 回显错误消息

How to echo an error message using $_GET in php

[已解决]:这段代码是有效的,我在浏览器中显示我的 .php 页面的旧版本时犯了一个错误,它没有下面的更新代码。我想将我的 php 代码产生的错误 'error' 回显到登录表单的页面中,该错误出现在 URL仅,例如:signin.php?error=用户名不正确。所以我希望相同的消息出现在登录表单本身中,我尝试使用 $_GET['error'] 并在我的表单中回显它,

            <form method="post" onsubmit=" return formSubmit() " action="signinphp.php">
                <div class="userimage">
                    <img class="userlogo" src="image/userlogo.png" alt="Picture- User Profile picture">
                </div><br>
             <?php if (isset($_GET['error'])){?>
                <p class="error"><?php echo $_GET['error'];?></p>
            <?php } ?>
                <div class="error" id= "errorMsg"></div> <br>
                <div class="error" id= "errorMsg1"></div>
                 <div class="field">
                    <label class="stafflabel">  Staff Name  </label>
                    <input class="area" placeholder="staffmember or admin" onclick=" return userValidation()" onchange=" return userValidation()" id="staff" name="staffname" type="text" >
                 </div> <br>
                 <div class="error" id= "errorMsg2"></div>
                 <div class="field">
                    <label class="passlabel">   Password    </label>
                    <input class="area" placeholder="password" onclick=" return userValidation()" onchange=" return userValidation()" id="pass" name="password" type="password" >
                 </div><br>
                
                    <div class="checkbox">
                       <input type="checkbox" class="remember-me">
                       <label class="remember" for="remember-me">Remember me         </label>   
                       <a class="pass-link" href="#"> Forgot password?</a>
                    </div><br><br><br>
                    
                 <div class="field">
                    <input class="btn" onclick="check(this.form)" type="submit" value="Sign in">
                 </div> <br>
                 <div class="account-link">
                    Didn't create an account yet? <a href="#">Create Account</a>
                 </div>
            </form>

PHP

<?php
if (isset($_POST['staffname'])&& isset($_POST['password'])){
    function validate($data){
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
}
$staffname = validate($_POST['staffname']);
$pass= validate($_POST['password']);
if (empty($staffname)){
    header("Location:signin2.php?error=Staff name and password are required!");
    exit();
} else if (empty($pass)){
    header ("Location:signin2.php?error=Staff name and password are required!");
    exit();
} else {


        if ($staffname == "staffmember" && $pass== "letmein!123"){
            echo "Logged in!";
            header("Location: log-it-reportsbeta.php");
            exit(); 
            }
     else if ($staffname == "admin" && $pass== "heretohelp!456"){
                echo "Logged in!";
                header("Location: sql_select_updated.php");
                exit();    
            }
            
        }



    }

     



else{
header("Location: signin2.php");

exit();
}

但是没有用,我在下面提供了我的表单代码和表单验证的php代码,请看一下,谢谢。

您可能想使用额外的条件来获取错误消息,而不是在 URL.

中传递整个错误字符串

在您的 php 脚本中:

if (empty($staffname)){
    header("Location:signin2.php?error=1");
    exit();
} else if (empty($pass)){
    header ("Location:signin2.php?error=2");
    exit();
} 

在您的表单中:

if (isset($_GET['error'])) {
    if ($_GET['error'] == 1) {
        $message = "Staff name and password are required!";
    }
    else if ($_GET['error'] == 2) {
        $message = "Another error message";
    }
    echo '<p class="error">'.$message.'</p>';
}