phphtml

PHP Function gets ignored when 'action = filename.php' is in <form>


So basically, my form "ignores" the PHP Function that detects any input errors when the 'action' has a destination. The form straight activates the action. (e.g. action = "adminPage.php">, however, there isn't a problem when the action is just empty (e.g. action = ""). How do I solve this? I want the error detection to be output before the form is validated to go adminPage.php

Here is my code:

<?php 
require_once('includes/helper.php');

function detectError()
{   
    global $id, $password;
    $error = array();
   
    //validate StudentID
    if($id == null)
    {
        $error['id'] = 'Please enter <strong>Admin ID</strong>.'; 
    }
    else if(!preg_match('/^[A]\d{4}$/', $id))
    {
        $error['id'] = '<strong>Admin ID</strong> is of invalid format. Format: A1234.'; 
    }

    if($password == null)
    {
        $error['password'] = 'Please enter <strong> Password </strong>.';
    }
    else if(strlen($password) < 8 || strlen($password) > 15)
    {
        $error['password'] = '<strong>Password</strong> must be between 8 to 15 characters.';
    }
    else if(!preg_match('/^\w+$/', $password))
    {
        $error['password'] = '<strong>Password</strong> must contain only alphabet, digit and underscore.';
    }
    
    return $error;
}
?>

<html>
    <link rel="stylesheet" href="CSS/login.css">
    <head>
        <meta charset="UTF-8">
        <title>Login Page for Admin</title>
    </head>
    <body>  
        <?php 
        $error = detectError(); 

        if(!empty($_POST))
        {
            $id = strtoupper(trim($_POST['id']));
            $password = trim($_POST['password']);

            $error = detectError();
            if(empty($error))
            {
                $id = $password = null;
            }
            else
            {
                echo '<ul class ="error">';
                foreach($error as $value)
                {
                    echo "<li>$value</li>";
                }
                echo '</ul>';
            }
        }
        ?>
        <div class="center">
            <div class="container">
                <form action="adminPage.php" method="post">
                    <table cellpadding="5" cellspacing="0">
                        <tr>
                        <h1 style="text-align: center; font-family: 'Courier New', monospace; ">Admin Login</h1>
                        </tr>
                        <tr>
                            <div style='text-align: center;'>
                            <img src='images/logo.png' width=180px height='180px'>                                
                            </div>
                        </tr>
                        <tr> 
                            <td><label for="studentID">Admin ID :</label></td>
                            <td>
                                <?php htmlInputText('id', $id, 5, 'Enter Admin ID Here...') ?>
                            </td>
                        </tr>
                        <tr>
                            <td><label for="password">Password :</label></td>
                            <td>
                                <?php htmlInputPassword('password', $password, 15, 'Enter Password Here...') ?>
                            </td>
                        </tr>
                    </table>

                    <input type="submit" name="insert" value="Log In" id="buttons"/>
                    <input type="reset" value="Reset" onclick=""location='<?php echo $_SERVER["PHP_SELF"]?>'" id='buttons'/>
                    
                    <div style="text-align: center">
                        <a href="login.php">Click here if you wish to log in as User</a>
                    </div>
                </form>       
            </div>
        </div>
    </body>  
</html>

Any help is appreciated, thank you!!


Solution

  • This code has to be refactored. I would suggest you to separate this validation on another file. But to answer your question, you can use header("Location: ...") to redirect after validating. See here

    <?php
    function detectError() {
      return; //
    }
    
    $errors = detectError();
    if (!$errors) { // you may have your own validation if statement here
      header('Location: adminPage.php'); // if no error, redirect to adminPage.
    }
    ?>
    <form action="">
    

    By the way, empty action means, you are submitting the form to the PHP script of the current URL.