473,785 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best test for empty form value ?

Hi guys,

What's the best way to test for an empty form value ? I am doing it like
this now:

$test = $_POST['inputTest'];
if(strlen($test ) < 1)
// it is empty !

Maybe I can just go:

if($test == '') // that's two single-quotes with nothing in between
// it is empty !

At least I avoid a function call that way. Maybe there is a better way ?

Take care,
Cyrus
Jul 17 '05 #1
4 2304
Either way should work find, the second way may have less load on the
server, but honestly neither of them would cause too much... So
whatever is your preference. I ususally do it the second way and never
had a problem with it.

Jul 17 '05 #2
> What's the best way to test for an empty form value ? I am doing it like
this now:

$test = $_POST['inputTest'];
if(strlen($test ) < 1)
// it is empty !

Maybe I can just go:

if($test == '') // that's two single-quotes with nothing in between
// it is empty !


It has been my experience that the function "empty()" has not failed me.

If you have a string, "" or '' or "0" is empty.
If you have a numeric variable, 0 is empty
Arrays with no elements is empty
A class property that has been declared but not used (set) is empty
A boolean variable that is FALSE is empty
NULL values are empty
Any object with no properties set is empty

http://www.php.net/empty

_______________ _______________ ______
Wil Moore III, MCP | Integrations Specialist | Assistant Webmaster
Jul 17 '05 #3
$test = $_POST['inputTest'];
if(strlen($test ) < 1)
// it is empty !

Maybe I can just go:

if($test == '') // that's two single-quotes with nothing in between
// it is empty !


Also, do not forget, if you are testing for a key in an array such as your example: $_POST['inputTest'];
you can do isset($_POST['inputTest']) or you can use array_key_exist s('inputTest', $_POST)

If you KNOW the key is there, but you just want to know if it has a NULL or "empty" value, your best bet is to run it through the empty() function.

_______________ _______________ ______
Wil Moore III, MCP | Integrations Specialist | Assistant Webmaster
Jul 17 '05 #4
The foolproof way to check for an empty string is always strlen($var) < 1.
You may also want to use trim() to remove any leading/trailing spaces.

--
Tony Marston

http://www.tonymarston.net

"Cyrus D." <sa***@invalid. org> wrote in message
news:p7******** *************@n ews4.srv.hcvlny .cv.net...
Hi guys,

What's the best way to test for an empty form value ? I am doing it like
this now:

$test = $_POST['inputTest'];
if(strlen($test ) < 1)
// it is empty !

Maybe I can just go:

if($test == '') // that's two single-quotes with nothing in between
// it is empty !

At least I avoid a function call that way. Maybe there is a better way ?

Take care,
Cyrus

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
62499
by: n_o_s_p_a__m | last post by:
My xml doc has many <title></title> and <title> in it, meaning the nodes have no content (although some do). How can I test for this? I tried title (doesn't work) I tried //title (doesn't work) I tried //title (doesn't work) I tried //title (doesn't work) Any suggestions welcome.
6
23626
by: WindAndWaves | last post by:
Hi Folks I have inhereted a script that I understand reasonably well, I just do not understand !/^\d+$/.test(el.value) what the hell does that mean? Below is the script (there are really three and they validate a four items
136
9457
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
7
8667
by: Gertjan van Heijst | last post by:
Hi, I really hope someone can help me because I've already spend 2 days on this problem and I'm not getting anywhere. I think the problem is that I don't really understand how text boxes store 'empty' values. I'm trying tot do the following. I have a continous sub form that lists transactions. On the top level form I have some text boxes to let the user specify the transactions between which dates should be listed. To do this I have...
6
2543
by: EDOnLine | last post by:
New asp.net/webform user question...... I am trying to use the Custom Validator control to check to see if either of two fields on my form have been filled in. At this point all I am interested in is if the user has entered a value into EITHER input box (not left blank). Below is my control code and also my function. I guess my question is, What should I be testing for in my function? I have tried many things (checked the length of...
0
4248
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET Applications and owner of Access Microsystems. Doug can be reached at doug@accessmicrosystems.com. --------------------------------------------------------------------------------
5
2397
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix it. It reads in the file and sorts out the files into the four different lists. F.txt int main 2 " " help
7
6999
by: h7qvnk7q001 | last post by:
I'm trying to implement a simple server-side form validation (No Javascript). If the user submits a form with errors, I want to redisplay the same form with the errors highlighted. Once the form is correct I need to submit to another page that uses the form data. I first tried making the form submit action= field point to the same file. When the form was correct, I tried loading the next page by using <META http-equiv refresh>. But...
1
2825
by: Muchach | last post by:
Hello, Ok so what I've got going on is a form that is populated by pulling info from database then using php do{} to create elements in form. I have a text box in each table row for the user to enter input. I need to take this user input and put it back into the database. What would be the best method to do this. I can't use a normal post because the name of the text box is the same for each table row. I've heard that posting the...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.