473,396 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2242
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.com
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_globals - 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_globals=On" threads again...

--
Justin Koivisto, ZCE - ju****@koivi.com
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
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....
2
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
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. ...
6
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...
6
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...
6
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...
2
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...
6
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.