Page 1 of 1

if(empty())

Posted: Sat Apr 22, 2006 4:23 pm
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.

Posted: Sat Apr 22, 2006 4:27 pm
by John Cartwright

Code: Select all

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

Also, remember to quote your array indices

Code: Select all

echo "Hi " . $_POST['username'] . "!";

Posted: Sat Apr 22, 2006 4:32 pm
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!!!!

Posted: Sat Apr 22, 2006 10:21 pm
by timvw
Better use htmlentities to make sure the data is ready for use in html...

Posted: Sat Apr 22, 2006 10:28 pm
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.

Posted: Sun Apr 23, 2006 2:01 pm
by andym01480
Does that mean isset() is better?

Posted: Sun Apr 23, 2006 3:56 pm
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'].