0

This is my PHP and HTML:

<?php
if(isset($_POST['submit'])){
$user = $_POST['username'];
$pwrd = $_POST['pwrd'];
include('db_connect.php');
if(empty($user) || empty($pwrd)){
echo 'Please enter all the required information to proceed';
}
}
?>

<form action="login.php" method="post">
<input type="text" name="username" placeholder="Username">
<p></p>
<input type="password" name="pwrd" placeholder="Password">
<p></p>
<input type="submit" value="Log In">
</form>

Currently when someone leaves one of the text input fields empty, it's meant to display the error 'Please enter all the required information to proceed' - it doesn't seem to be doing that though.

Help would be appreciated.

1 Answer 1

3

That's because of this conditional statement

if(isset($_POST['submit']))

There is no element bearing the name attribute "submit" for it.

Name your submit input:

<input name="submit" type="submit" value="Log In">

Error reporting would have thrown something to the effect of

undefined index submit - had it been enabled and to display them or logged.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


Having placed an else{...} for the first conditional statement, would have echoed "Not set", as per using your existing code:

<?php
if(isset($_POST['submit'])){
$user = $_POST['username'];
$pwrd = $_POST['pwrd'];
include('db_connect.php');
if(empty($user) || empty($pwrd)){
echo 'Please enter all the required information to proceed';
}
}

else{ echo "Not set"; }

?>
Sign up to request clarification or add additional context in comments.

3 Comments

It seems error reporting didn't display anything though - how do I make it display errors?
@JugglingBob I've made an edit to my answer for that.
@JugglingBob question should be marked as solved. Otherwise, others may think the question is still open.

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.