-2

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!!

gp_sflover
  • 3,460
  • 5
  • 38
  • 48
  • 1
    What have you tried to resolve the problem? If you send the request to another file that does **not** contain the same checks, it's obvious that they are not run – Nico Haase Aug 05 '21 at 13:46
  • 2
    `The form straight activates the action`...yes that's what it is supposed to do. It will make a request to adminpage.php directly, so the code in the current script will not be executed again. If you want it to validate, then either move the validation code to adminpage or, probably better, post the form back to the current page and then redirect to adminpage if validation succeeds – ADyson Aug 05 '21 at 13:49

2 Answers2

1

Welcome to stackoverflow. 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 on the current file.

smzapp
  • 809
  • 1
  • 12
  • 33
-1

I have found a solution - I just added javascript location.href="filename.php" inside if(empty($error)) while closing and opening new PHP instances inside.. not sure if it's the correct way, but it works! for now

like:

if(empty($error))
{
     $id = $password = null;
     ?>
<script type="text/javascript">location.href = 'adminPage.php';</script>
     <?php
}

while

<form action="" method="post">

is now empty. Thanks for the help guys T_T and sorry for the dumb question lol!

  • 2
    This is a very bad solution ... For example, i can turn off Javascript client side. Please try to fix your problem instead of using dirty fixes and posting them here. There are no dumb questions by the way. – Gert B. Aug 05 '21 at 14:17
  • Why not **properly** redirect the user if no errors occur? – Nico Haase Aug 05 '21 at 14:21
  • ahaha, thank you for the info.. But I can't think of how to do it! I'm still a student in school starting to learn PHP, but that doesn't excuse the bad code practice! I'll take this as a lesson moving on, thanks! – hainanjifan Aug 05 '21 at 14:26
  • See https://stackoverflow.com/questions/768431/how-do-i-make-a-redirect-in-php for how to redirect in a better way – ADyson Aug 05 '21 at 14:48