Checking to see if Field was Submitted empty

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
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Checking to see if Field was Submitted empty

Post by anthony88guy »

When I use the following script, its doesnt return "Please Fill In all the Fields." when the field is empty. If you need all the code tell me.
Last edited by anthony88guy on Thu May 12, 2005 5:45 pm, edited 1 time in total.
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post by thegreatone2176 »

if you want to check for something being posted use

Code: Select all

if ($_SERVER['REQUEST_METHOD'] == 'POST')
also you have || to check for empty fields which means if they are both empty it will still update
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post by thallish »

hi

i would use isset() for checking if it exist before stripping the $_POST array and then check to see if it's empty

Code: Select all

if(isset($_POSTї'variableName']))
{
    if($_POSTї'variableName'] == &quote;&quote;)
    {
      echo &quote;Please fill the field that corresponds to the relevant variable name&quote;;
    }
    else
    {
      // do your thing here
    }
} 
else 
{

echo &quote;Nothing Posted&quote;;

}
regards
thallish
Last edited by thallish on Mon Apr 25, 2005 2:19 am, edited 1 time in total.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

thegreatone2176 wrote:also you have || to check for empty fields which means if they are both empty it will still update
I think what he's trying to say is that if one is empty and the other isn't then it'll still update, which is the case. Instead you should be using &&.

Thallish, what's the reasoning behind checking to see if it exists first, and only if it does checking to see if it has a value?
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

RE:

Post by harrisonad »

When a form is submitted using POST method, all of it's member inputboxes, selectionboxes, and other elements are also submitted with their values in it. Now, if the user didn't completely fillup the form, $_POST variables will still be sent and contains their own default values.

So, what i am trying to explain is...checking for the existence of $_POST as in:

Code: Select all

if($_POST){
 // do stuff
}
... doesn't ensure you that the form is empty.

Instead, get all the $_POST variables you need:

Code: Select all

$id = $_POST['id'];
$operation = $_POST['operation'];
// etc.
and then check each variables for NULL values:

Code: Select all

if($id && $operation){
// do stuff...
}
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Thank you, harrisonad

Now it works.

Edit: Doesnt work right.
Last edited by anthony88guy on Mon Apr 25, 2005 8:07 am, edited 1 time in total.
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post by thallish »

nigma wrote: Thallish, what's the reasoning behind checking to see if it exists first, and only if it does checking to see if it has a value?
For error protection, if all the relevant variables dosen't exist then it can make errors in your code. if for exampel some visitor come from a site other than the site with the form and you make a lot of work on the variables in the $_POST array then you would get errors cause the don't exist. thats the reasoning. but if anybody knows a better method then be sure to "post reply"
harrisonad wrote:
Instead, get all the $_POST variables you need:

Code: Select all

$id = $_POST['id'];
$operation = $_POST['operation'];
// etc.
and then check each variables for NULL values:

Code: Select all

if($id && $operation){
// do stuff...
}
isset() does the same to my understanding and you would have saved some effort.

regards thallish
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

besides others' suggestions, you can also consider either:

1- use session to save any info had been filled out by the user. this to prevents him from writing all over again if he missed to fill out 1 field,

2- or use javascript to check if all required fields have been filled onSubmit action.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

re

Post by harrisonad »

With due respect, thallish, isset() function tells if a variable has been defined. as the holy scripture said:

Code: Select all

bool isset ( mixed var ї, mixed var ї, ...]])
 - Returns TRUE if var exists; FALSE otherwise.
but it will also return true in submitted empty form.

read the bible.
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post by thallish »

harrisonad wrote:With due respect, thallish, isset() function tells if a variable has been defined. as the holy scripture said:

Code: Select all

bool isset ( mixed var ї, mixed var ї, ...]])
 - Returns TRUE if var exists; FALSE otherwise.
but it will also return true in submitted empty form.

read the bible.
maybe I didn't make myself clear, but if you try to read my reply then you would see that i actually write that it is used to check if it exist. thats why the check to see if the variable is empty is used

thallish
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Sorry guys but it doesnt work. The following code will return "Please Fill In all the Fields." at everything you submit at it. It will never return "Farm $fuser was updated." This is <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> me off now. In other pages I used

Code: Select all

if($var != "")
to see if the field was emtpy. But in this case that returns "Please Fill In all the Fields.". I need some help.
Last edited by anthony88guy on Thu May 12, 2005 5:59 pm, edited 1 time in total.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

anthony88guy, in the code you posted there is nowhere you check for the vars being empty. you just check for their existence which they will, even if they are submitted empty. so change you line 12's if condition to include vars being blank.

Code: Select all

if(trim($fuda) != '' &amp;&amp; trim($fuparmysize) != '') {
i trimmed them to remove any spaces from them before checking.
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Same error. Thats the basicly the same thing I had before. I'll post all the code. We are looking at lines 108-115.

Click on "" on the navigation, then in the form just press "" Now hopefully some "" should come up. On the right their is an update button, Click it. Then play around with the form, doesnt work right. You cant harm anything since I will be dumping the tables. Thanks for the help and time you put into this.
Last edited by anthony88guy on Thu May 12, 2005 6:00 pm, edited 2 times in total.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Sorry for posting late... i was busy in my work
now there is a typo

Code: Select all

if(trim($fuda) != '' && trim($fuparmysize) != '')
should be

Code: Select all

if(trim($fupda) != '' && trim($fuparmysize) != '')
also print them out before checking them...
when i removed most of you extra stuff and corrected the above typo it started responding correctly...
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Thank you n00b Saibot, Now it offically works. I should of caught the incorrect variable.
Post Reply