if(empty())

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

if(empty())

Post by SidewinderX »

i cant seem to figure out why this isnt working, any ideas?

Code: Select all

<?php

if(empty($username))
	{
		echo "Enter Your Name:
		<form method='POST' action=" . $_SERVER[PHP_SELF] . ">
		<input type='text' name='username'>
		<input type='submit' value='Log In'>
		</form>";
	}
else 
	{
		echo "Hi " . $_POST[username] . "!";
	}

?>
thanks-side.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

if(empty($_POST['username']))
??

Also, remember to quote your array indices

Code: Select all

echo "Hi " . $_POST['username'] . "!";
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Two mistakes I could see...
if empty needed $_POST
and $_SERVER['PHP_SELF'] needed quotes

Try

Code: Select all

<?php 

if(empty($_POST['username'])) 
{ 
echo "Enter Your Name: 
<form method='POST' action=" . $_SERVER['PHP_SELF'] . "> 
<input type='text' name='username'> 
<input type='submit' value='Log In'> 
</form>"; 
} 
else 
{ 
echo "Hi " . $_POST['username'] . "!"; 
} 

?>
Sorry to repeat, beat me to posting!!!!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Better use htmlentities to make sure the data is ready for use in html...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Also remember that empty() matches a bunch of values ('', null, 0, false..) some of which may not be what you are looking for.
(#10850)
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Does that mean isset() is better?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

It means that they are different (or not?) Find out yourself:
http://www.php.net/isset
http://www.php.net/empty
http://www.php.net/exists

Btw, i suggest that you use # as target instead of $_SERVER['PHP_SELF'].
Post Reply