Checking for an empty html form submission

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
croniccoder
Forum Commoner
Posts: 27
Joined: Fri Jul 07, 2006 10:45 am

Checking for an empty html form submission

Post by croniccoder »

I am checking for an empty html form that is submitted using <input name="title" type="text>.

Code: Select all

$title  = $_POST['title'];

if ($title == ' ')
{
     print "<p>This is a required field. Please go back and fill it out.</p>";
}
else{
         // process stuff
  }
But this is not working and I can't figure out why. If I submit the title form and it is empty, the code still goes to the //process stuff.

any thoughts?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Code: Select all

empty()
croniccoder
Forum Commoner
Posts: 27
Joined: Fri Jul 07, 2006 10:45 am

Post by croniccoder »

thanks man!

I tried the function, but I must have used it incorrectly the first time. It works fine now!
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

croniccoder wrote:thanks man!

I tried the function, but I must have used it incorrectly the first time. It works fine now!
you might want to trim() the input first...because empty will return false on " " (space).
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

This is how I would check for an empty value..

Code: Select all

if ((!isset($_POST['title'])) or (trim($_POST['title']) == ''))
{
     print "<p>This is a required field. Please go back and fill it out.</p>";
} else {
    $title  = $_POST['title'];
         // process stuff
  }
Post Reply