469,288 Members | 2,320 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,288 developers. It's quick & easy.

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 12505
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.