Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2004, 01:41 AM   PM User | #1
vern
New Coder

 
Join Date: Sep 2004
Location: Minnesota, USA
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
vern is an unknown quantity at this point
Form validation

I want to make sure that forms that are being submitted have content. How do we check if a field is empty when it is submitted?

I'm guessing

Code:
if (isset($_POST['item']))
{
   do this ....
}
else
   echo 'error';
What is the correct way of making sure a field is not empty?
vern is offline   Reply With Quote
Old 09-24-2004, 02:08 AM   PM User | #2
litebearer
Regular Coder

 
Join Date: Apr 2004
Posts: 287
Thanks: 0
Thanked 21 Times in 21 Posts
litebearer is on a distinguished road
A note...

this will result in 'Set' being echoed
PHP Code:
$var "";
if (isset(
$var)) {
  echo 
"set";
} else {
echo 
"not set";

you might also check to see that the variable actually contains something

ie..
PHP Code:

$var 
$_POST['var'];

if (
strlen(trim($var)) <0) {
  
// the variable is either empty or not set
] else {
  
// there is something in the variable

Lite...
litebearer is offline   Reply With Quote
Old 09-24-2004, 02:32 AM   PM User | #3
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
Or, to make it easier for yourself, use the empty() function

if(empty($_POST['item'])){
echo 'Item is empty';
}else{
echo 'Item is filled';
}
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 09-24-2004, 02:35 AM   PM User | #4
vern
New Coder

 
Join Date: Sep 2004
Location: Minnesota, USA
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
vern is an unknown quantity at this point
Looks good. Thanks!
vern is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:09 PM.


Advertisement
Log in to turn off these ads.