1

The following code does not work as intended, when the submit button of the form is clicked it with no data entered it goes to blog.php instead of showing the error above the form?

<?php 

session_start();

include_once('connection.php');



if (isset($_SESSION['logged_in'])){
    //display index
} else {
    if (isset($_POST['username'], $_POST['password'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];

        if (empty($username) or empty($password)) {
            $error = 'All fields are required!';
        }
    }
}
?>

linked with the following html form

  <?php if (isset($error)) { ?>
    <small style="color:#aa0000;"><?php echo $error;  ?> </small>
  <?php } ?>


  <form action="blog.php" method="post">
    <input type="text" name="username" placeholder="username" />
    <input type="password" name="password" placeholder="password" />
    <input type="submit" value="login" />
  </form>
5
  • the code is from admin.php Commented Apr 30, 2015 at 0:31
  • You could redirect back to the login page from the PHP that tests for the two inputs with a header('Location: login.php?error='.$error) if an error occurs. Commented Apr 30, 2015 at 0:32
  • i dont think $error is receiving a value whatever is wrong Commented Apr 30, 2015 at 0:33
  • It won't receive a value because blog.php doesn't know the variable $error from admin.php Commented Apr 30, 2015 at 0:35
  • yeah that was silly, getting late... thanks guys Commented Apr 30, 2015 at 0:37

1 Answer 1

1

If the actual validation is being done in admin.php, shouldn't the action be pointing to admin.php?

  <?php if (isset($error)) { ?>
    <small style="color:#aa0000;"><?php echo $error;  ?> </small>
  <?php } ?>


  <form action="admin.php" method="post">
    <input type="text" name="username" placeholder="username" />
    <input type="password" name="password" placeholder="password" />
    <input type="submit" value="login" />
  </form>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.