Home > Back-end >  How to echo an error message using $_GET in php
How to echo an error message using $_GET in php

Time:03-06

[SOLVED]: This code is functional, i made a mistake by displaying an old version of my .php page into the browser, which didn't have the updated code below. I would like to echo an error 'error' produced from my php code which is meant to validate a sign in form, into the page of the sign in form, the error appears in the URL only , for example : signin.php?error= User name is incorrect. So i would like the same message to appear within the sign in form itself , i tried to use $_GET['error'] and echo'ed it within my form,

            <form method="post" onsubmit=" return formSubmit() " action="signinphp.php">
                <div >
                    <img  src="image/userlogo.png" alt="Picture- User Profile picture">
                </div><br>
             <?php if (isset($_GET['error'])){?>
                <p ><?php echo $_GET['error'];?></p>
            <?php } ?>
                <div  id= "errorMsg"></div> <br>
                <div  id= "errorMsg1"></div>
                 <div >
                    <label >  Staff Name  </label>
                    <input  placeholder="staffmember or admin" onclick=" return userValidation()" onchange=" return userValidation()" id="staff" name="staffname" type="text" >
                 </div> <br>
                 <div  id= "errorMsg2"></div>
                 <div >
                    <label >   Password    </label>
                    <input  placeholder="password" onclick=" return userValidation()" onchange=" return userValidation()" id="pass" name="password" type="password" >
                 </div><br>
                
                    <div >
                       <input type="checkbox" >
                       <label  for="remember-me">Remember me         </label>   
                       <a  href="#"> Forgot password?</a>
                    </div><br><br><br>
                    
                 <div >
                    <input  onclick="check(this.form)" type="submit" value="Sign in">
                 </div> <br>
                 <div >
                    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();
}

but it did not work , i provided my form code below and the php code for the form validation, please take a look , thank you.

CodePudding user response:

You may wanna use an extra condition to get an error message instead of passing the whole error string in your URL.

In your php script :

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

In your form :

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 >'.$message.'</p>';
}
  • Related