473,659 Members | 2,944 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query about empty POST arrays

Hi all,

I started programming in PHP recently and have a query about empty
$_POST arrays. I can see two scenarios when this could happen.

1. When some tries to directly load the page to which data is being
posted (for example, opening www.foo.com/xyz.php directly when a form
action is xyz.php)
2. When the user clicks the submit button without entering anything.

I am aware that Javascript validation can take care of the second case,
but it's unreliable. What is the accepted way of handling the above
situations on the server side?
Do I just reload the calling page if the array is empty?

Thanks,
Thejo

Oct 6 '05 #1
5 2255
Thejo wrote:
Hi all,

I started programming in PHP recently and have a query about empty
$_POST arrays. I can see two scenarios when this could happen.

1. When some tries to directly load the page to which data is being
posted (for example, opening www.foo.com/xyz.php directly when a form
action is xyz.php)
2. When the user clicks the submit button without entering anything.
Then (if method=post), the submit button itself is ported (it has a name
and a value). I am aware that Javascript validation can take care of the second case,
but it's unreliable. What is the accepted way of handling the above
situations on the server side?
Do I just reload the calling page if the array is empty?

Thanks,
Thejo


It is not uncommon to omit the action attribute on the form of a PHP
webpage. This means that the same page is used for displaying of the
form AND for processing the results. This can be handy for continuous
adding or giving the user a second change to correct the data posted.
Off course, an if-statement gives you the power to have the same page
behave totally different depending on what was posted.

Best regards
Oct 6 '05 #2

Dikkie Dik wrote:
Thejo wrote:
Hi all,

I started programming in PHP recently and have a query about empty
$_POST arrays. I can see two scenarios when this could happen.

1. When some tries to directly load the page to which data is being
posted (for example, opening www.foo.com/xyz.php directly when a form
action is xyz.php)
i generally use a hidden field to detect a submit.
2. When the user clicks the submit button without entering anything.
then the post array is not empty, the elements just contain empty
strings. the validation routine of the form data should take care of
that.

Then (if method=post), the submit button itself is ported (it has a name
and a value).


careful here, ie does NOT submit the button if the form is submitted
using the return key.
I am aware that Javascript validation can take care of the second case,
but it's unreliable. What is the accepted way of handling the above
situations on the server side?
Do I just reload the calling page if the array is empty?

Thanks,
Thejo


It is not uncommon to omit the action attribute on the form of a PHP
webpage. This means that the same page is used for displaying of the
form AND for processing the results. This can be handy for continuous
adding or giving the user a second change to correct the data posted.
Off course, an if-statement gives you the power to have the same page
behave totally different depending on what was posted.


micha

Oct 6 '05 #3
Exactly.

if ($_POST['submit']) // Gives you the validation that the submit
button itself has been pushed,

then to detect an empty field

$myfield = trim($myfield);
if (!$myfield)
{
echo "error, no data provided";
}

Oct 6 '05 #4
chotiwallah wrote:
Dikkie Dik wrote:
Thejo wrote:
2. When the user clicks the submit button without entering anything.
then the post array is not empty, the elements just contain empty
strings. the validation routine of the form data should take care of
that.


Not true... if the form is made up of all radio buttons and/or
checkboxes (with no default selected) AND the submit button doesn't have
a "name" attribute, $_POST will be empty (as in $_POST=array() type of
empty).
Then (if method=post), the submit button itself is ported (it has a name
and a value).


careful here, ie does NOT submit the button if the form is submitted
using the return key.


Most browsers don't because the button wasn't clicked...

--
Justin Koivisto, ZCE - ju****@koivi.co m
http://koivi.com
Oct 6 '05 #5
Dr******@gmail. com wrote:
Exactly.

if ($_POST['submit']) // Gives you the validation that the submit
button itself has been pushed,

then to detect an empty field

$myfield = trim($myfield);
if (!$myfield)
{
echo "error, no data provided";
}


Assuming that $myfield was a text input, a hidden field, a select
element, a textarea, etc. Checkboxes that aren't selected and radio
button groups that don't have a value chosen will not even exist in the
$_POST array... Also, you should have used:

$myfield = trim($_POST['myfield']);

Get out of the habit of using register_global s - especially when trying
to give examples in a newsgroup. Too many newbies will wonder why it
isn't working for them, then we'll get the whole "if
register_global s=On" threads again...

--
Justin Koivisto, ZCE - ju****@koivi.co m
http://koivi.com
Oct 6 '05 #6

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

Similar topics

13
2881
by: dogu | last post by:
Noob alert. Code is below. File is saved as a .php. What I'm trying to do: User uses 'select' box drop down list to pick a value. Value ($site) is derived from a db query. This works fine. Value selected is used as the 'where' clause of the 2nd query. If $site is a single word, the 2nd query works like a charm. If $site is more than one word (has spaces), the query returns a null
2
1717
by: Sebastian | last post by:
The following query needs about 2 minutes to complete (finding dupes) on a table of about 10000 addresses. Does anyone have an idea on how to speed this up ? Thanks in advance !!! Sebastian
14
3881
by: Dave Thomas | last post by:
If I have a table set up like this: Name | VARCHAR Email | VARCHAR Age | TINYINT | NULL (Default: NULL) And I want the user to enter his or her name, email, and age - but AGE is optional. My insert would look something like:
6
29932
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access newsgroups, the access support centre, I can seem to find no similar situation. I am not using any references, or VBA at all in the first place. I am trying to set up a simple (or so I thought) query to work with the text of two tables. ...
6
7114
by: Andy | last post by:
Hello, I am having many problems with setting up a parameter query that searches by the criteria entered or returns all records if nothing is entered. I have designed an unbound form with 3 fields on it: Date (DateSpan1 and DateSpan2), Originator, and GroupName. I have added a button that triggers a query and uses those fields as its parameter criteria to populate a form. The user must be allowed to either enter all of the
6
10748
by: Sparticus | last post by:
I have a database that isn't very big. It has about 2400 rows in it. I try and do an update like this one below (it looks big, but it's really not if you look at it) : UPDATE jobs SET prodduedate = '1/5/2005', clientduedate = '1/5/2005', digitizingcnd = 'x', digitizingcndnonsub = '', digitizingintl = '', retrieval = '', wellsitetape = '', boardsubmissions = '', hardcopylogs = '', burningcd = '', las20 = '', metric = '', tops = '',...
2
2065
by: chris_fieldhouse | last post by:
Hi, I'm maintaing a website for a group and we have several email addresses feeding into one inbox, so I want to find a way of finding all the emails that were sent to a particular address either via "TO" or "CC" (not interested in "BCC") imap_search seems to be the solution, but there are several issues. 1. I can get imap_search($conn, "TO test_address"); to work, and imap_search($conn, "CC test_address");, but not a combined 'OR'...
6
2794
by: Vinit | last post by:
Hi I am passing an arraylist to a c#/.net webmethod from a perl client using soap:lite. The trace shows the elements in the xml request. The arraylist input in the webmethod however does not contain any values and is empty. It works fine with arrays....but NOT arraylists....does anyone know WHY? and HOW TO SOLVE THIS ISSUE? Regards,
1
1688
by: iwl | last post by:
Hi, I've tryed to save some data containing empty arrays (array('f')) in a shelve. It looks like the shelve has some problems with empty arrays, get allways: TypeError: ("'NoneType' object is not iterable", <type 'array.array'>, ('f', None))- Messages when dealing with the readed back shelve. Seems like I have to avoid empty arrays in a shelve.
0
8428
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
8335
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
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
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
6179
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
4175
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
2752
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
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
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.