if(!$_POST['username'] || ['password']) {
  	 die('Error: Username / Password field was blank');
	 }

What is the mistake in the code above. I want the syntax to check if either one of the fields are blank.

Recommended Answers

All 19 Replies

if(!$_POST['username'] || !$_POST['password']) {
  	 die('Error: Username / Password field was blank');
	 }

That doesn't check for blank. Try

if(empty($_POST['username'])){
   echo "Empty username!";
}
//or you can also do it this way
if($_POST['username']==""){
 echo "Empty username!";
}

Thanks buddy, just the code I'm looking for.

:) You are welcome!

if($_POST['username']==""){
	echo "Username field is EMPTY!";
}
elseif($_POST['password']==""){
	echo "Password field is EMPTY!";
}
else($_POST['username']=="") && ($_POST['password']=="") {
  	 echo "Username & Password field are EMPTY!";
}

Whats wrong with the whole code?

if($_POST['username']==""){
  echo "Username empty!";
} 
if($_POST['password']==""){
 echo "Password empty!";
}
if($_POST['username']=="" && $_POST['password']=="") {
 echo "Both username and password is empty!";
}

This will do the same as what you are trying to do.

if($_POST['username']==""){
  echo "Username empty!";
} 
if($_POST['password']==""){
 echo "Password empty!";
}
if($_POST['username']=="" && $_POST['password']=="") {
 echo "Both username and password is empty!";
}

The looping doesn't work.

if(empty($_POST['username'])) {
die('Username field was blank');
}
if(empty($_POST['password'])) {
die('Password field was blank');
}
if(empty($_POST['username']) && empty($_POST['password'])) {
die('Both field was blank');
}

This is working but if both the username and password was blank, it can't display the 3rd condition's result. Instead of that, it echo back the 1st condition's result.

Thats because, It will exit when it encounters first if(empty($_POST)).

if(empty($_POST['username']) && empty($_POST['password'])) {
	echo "Both empty"; 
	exit;
} 
if(empty($_POST['username'])){
	echo "username empty!";
	exit;
} 
if(empty($_POST['pass'])){
	echo "pass empty!";
	exit;
}

First validate for both the fields. Then other fields.

if(empty($_POST['username'])) {
die('Username field was blank');
}
if(empty($_POST['password'])) {
die('Password field was blank');
}
if(empty($_POST['username']) && empty($_POST['password'])) {
die('Both field was blank');
}

and if I arrange it like this,

if(empty($_POST['username']) && empty($_POST['password'])) {
die('Both field was blank');
}
if(empty($_POST['username'])) {
die('Username field was blank');
}
if(empty($_POST['password'])) {
die('Password field was blank');
}

It works!

Exactly :P

We both post at the same time! Thanks.

can know how do I use css in php. I mean how do I use,

<div class = "error">
</div>

to style the echo msg .... e.g looping above

echo "<div class='error'>Text</div>"; If you want to use double quotes, you should escape those quotes. Eg. echo "<div class=\"error\">Text</div>";

if(empty($_POST['password'])) {
die('<div class="error">Password field was blank</div>');
}

When I used this code, it works, but the div tag gives a pinkish background whereas I've already set the background color in the css as black. Is the any other method using this css without a div tag.

I dont know! You should probably post this question in css forum !

manage to solve, my mistake. Anyway thanks for the support!

You are welcome! Glad you solved your problem!

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.