Reputation: 1
<?php
if( isset($_POST['pass']) && !empty($_POST['pass']) )
{
$pass = $_POST['pass'];
echo 'ok';
} else {
echo 'Enter Password';
}
?>
<form action="md.php" method="POST">
Password: <input type="password" name="pass"><br><br>
<input type="submit" value="Submit">
</form>
The problem is that when I run this code then by default I see the message Enter Password
displayed above the password field somehow the else condition is getting executed when I open this page on xampp server that else
block is supposed to execute when the user submits empty form and not when I open the page on the server.
Upvotes: 0
Views: 118
Reputation: 94642
The first time you run this form, from entering the url in a browser address bar of clicking on a link or menu item on another page, the $_POST
array will be empty.
So your code will cause the else
to be run and echo 'Enter Password';
will be executed
When you press the Submit button, assuming you enter a password, then the $_POST will exist and $_POST['pass']
will contain a value and you should see the ok
message. If you do not enter anything in the pass
password field you will of course see the echo 'Enter Password';
again
If you only want to see the message when you have forgotten to enter password, but pressed the Submot button, which is what I assume you are saying then try this change
<?php
if ( $_SERVER["REQUEST_METHOD"] == 'POST' ) {
// only set when the form was actually posted
// not set when you run form from a link or the address bar
if( !empty($_POST['pass']) ) {
$pass = $_POST['pass'];
echo 'ok';
} else {
// pressed submit without entering a password
echo 'Enter Password';
}
}
?>
<form action="md.php" method="POST">
Password: <input type="password" name="pass"><br><br>
<input type="submit" value="Submit">
</form>
Upvotes: 3
Reputation: 16772
The first time the page is opened, it gives you the else
block because the if statement
is false. So edit the else-if statement
as describe below, Furthermore, leave the action = ""
So it doesn't direct you to another page when you press the submit
button. Try this:
<?php
if( isset($_POST['pass']) && !empty($_POST['pass']) )
{
$pass = $_POST['pass'];
echo 'ok';
} else if( isset($_POST['pass']) && empty($_POST['pass']) ) {
echo 'Enter Password';
}
?>
<form action="" method="POST">
Password: <input type="password" name="pass"><br><br>
<input type="submit" value="Submit">
</form>
The above code should display the form, the very first time you open it and accordingly as you input/leave the password
field afterwards.
Upvotes: 0
Reputation: 382
Try changing your PHP code to:
<?php
if( isset($_POST['submit'])
{
if( isset($_POST['pass']) && !empty($_POST['pass']) )
{
$pass = $_POST['pass'];
echo 'ok';
} else {
echo 'Enter Password';
}
}
?>
This way the code will only run once you click the Submit button and not when you load the page.
Upvotes: 0
Reputation: 57709
Change the else
to:
} else if (isset($_POST["pass"]) && empty($_POST["pass"])) {
You code should have 3 paths:
Right now you only have 2 paths.
Upvotes: 0