Page 1 of 1

Checking for an empty html form submission

Posted: Fri Jul 21, 2006 1:29 pm
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?

Posted: Fri Jul 21, 2006 1:30 pm
by Luke

Code: Select all

empty()

Posted: Fri Jul 21, 2006 1:33 pm
by croniccoder
thanks man!

I tried the function, but I must have used it incorrectly the first time. It works fine now!

Posted: Fri Jul 21, 2006 3:45 pm
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).

Posted: Fri Jul 21, 2006 5:20 pm
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
  }