I am a newbie to php. I am simply trying to validate input from a login page. However I am always getting a positive boolean value when username is entered or not. Following is my code for the login page;

<form method = "post" action = "entering.php">
LOGIN <br />
<br />
User Name: <input type = "text" size = "10" id = "username" name = "username" /><br /><br />
Password: <input type = "password" size = "10" id = "password" name = "password"/><br />
<br />
<br />
<input type = "submit" value = "Submit" /> &nbsp <input type = "reset" />
</form>
</body>

</html>

Following the code for my action php page ie. entering.php

<?php


If (isset($_POST['username']))
	{
	echo "Username Entered"."<br />";
	
	}
Else
	{
	echo "Username Not entered"."<br />";
	}
?>

And I always get the message "Username Entered" even if the username is not entered.

Can any one help?

Dani AI

Generated

The observed behavior happens because the form field is sent as part of the POST whether it has text or not. The presence of the name="username" key in $_POST makes isset($_POST['username']) true even when the value is an empty string. As reported, that explains why the page always printed "Username Entered." was right to suggest checking for emptiness, and 's advice to echo what was posted is a good debugging step.

A simple, robust server-side pattern that trims whitespace and distinguishes an empty value:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    if ($username === '') {
        echo "Username Not Entered";
    } else {
        echo "Username Entered: " . htmlspecialchars($username, ENT_QUOTES, 'UTF-8');
    }
}
?>

Notes and troubleshooting tips:

  • empty() will treat the string "0" as empty; use trim() + strict === '' or strlen() when "0" must count as a valid value.
  • Client-side required (HTML5) improves UX but never replaces server-side checks.
  • Always escape output (e.g., htmlspecialchars) when echoing user input to avoid XSS.
  • Omitting the name attribute prevents a field from appearing in $_POST; the browser only sends named inputs.
  • Echoing the received value (as suggested) and checking $_SERVER['REQUEST_METHOD'] helps isolate form vs. direct script access.

For : isset() returns true if a variable/key exists and is not NULL; it does NOT mean the value contains non-whitespace text.

Recommended Answers

All 4 Replies

sometimes i found that the form will submit the field with nothing and add it to post array. you need to check and make sure the username is also not empty. you can do this by comparing it to an empty string or use the empty() function.
example:

if ( isset( $_POST['username'] ) && $_POST['username'] !== '' ) {
  echo 'Username Entered<br />';
}
else {
  echo 'Username Not Entered<br />';
}

Why not echo $username and see what is being posted

<?php
if isset($username) {echo $username; }
If (isset($_POST['username']))
 { echo $username."was posted";
 echo "Username Entered"."<br />"; }
Else  { echo $username."was posted";
 echo "Username Not entered"."<br />"; }
?>

Thank you very much for a quick response. It solved the issue. I used the empty() function along with isset() function. It did solve the issue as said.

isset means??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.