472,110 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

How do you test for blank form input?

This is a very simple question but after a long time I still can't find
an answer looking on my own. How do you test for no user input in a
form? Example:

<form action="index.php" method="get">
Type your first name:
<input type="text" name="FirstName">
<input type="submit" value="Submit">
</form>

<?php
if ($_GET["FirstName"] == "")
{
echo "No user input";
}
?>

Is this the correct way to always test if there was nothing input by
the user in the form? Should I test for nil or something else?
Thanks,
Chris

Aug 18 '05 #1
6 12607
try

if (trim($var) == "") //empty
else //not empy

Aug 18 '05 #2
trim() and also look at empty()

"Chris Portka" <ch*********@gmail.com> wrote in message
news:11********************@g43g2000cwa.googlegrou ps.com...
This is a very simple question but after a long time I still can't find
an answer looking on my own. How do you test for no user input in a
form? Example:

<form action="index.php" method="get">
Type your first name:
<input type="text" name="FirstName">
<input type="submit" value="Submit">
</form>

<?php
if ($_GET["FirstName"] == "")
{
echo "No user input";
}
?>

Is this the correct way to always test if there was nothing input by
the user in the form? Should I test for nil or something else?
Thanks,
Chris

Aug 18 '05 #3
Chris Portka wrote:
<?php
if ($_GET["FirstName"] == "")
{
echo "No user input";
}
?>


how about:

if (isset($_GET['FirstName']) && empty($_GET['FirstName'])) {
echo "yes, it's empty";
}

Specifically the "empty" function will be useful here, but sometimes its
good to check that the variable actually exists.

/Marcin
Aug 18 '05 #4
Chris Portka wrote:
This is a very simple question but after a long time I still can't find
an answer looking on my own. How do you test for no user input in a
form? Example:

<form action="index.php" method="get">
Type your first name:
<input type="text" name="FirstName">
<input type="submit" value="Submit">
</form>

<?php
if ($_GET["FirstName"] == "")
{
echo "No user input";
}
?>

Is this the correct way to always test if there was nothing input by
the user in the form? Should I test for nil or something else?
Thanks,
Chris

I also use NULL some times...eg.
if ($_GET["FirstName"] == NULL)
Aug 18 '05 #5
if ( !isset($_GET['whatever']) || ( empty($_GET['whatever']) &&
$_GET['whatever'] != 0) )

Aug 18 '05 #6
BKDotCom wrote:
if ( !isset($_GET['whatever']) || ( empty($_GET['whatever']) &&
$_GET['whatever'] != 0) )


php.net/empty

"... As of PHP 4, The string value "0" is considered empty..."
Aug 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Reply Via Newsgroup | last post: by
5 posts views Thread by Batezz | last post: by
4 posts views Thread by chirs | last post: by
2 posts views Thread by jervin via AccessMonster.com | last post: by
6 posts views Thread by 0utlawza | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.