473,398 Members | 2,343 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,398 software developers and data experts.

Notice: Undefined index...

I have since recently turned up my error reporting on a production site
to E_ALL to ensure I am using 'best practices' when writing out code.

So far it has helped me discipline myself with initializing my
variables. I am running into a problem with my $_POST array though.

I am self-referencing a form page for processing, so at the beginning I
am looking for the existence of certain $_POST variables for processing.

I am setting an 'action' field to 'login' for example.

I have done this on several occasions with no problems and even in this
case there is no fatal error. I am however receiving a NOTICE that my
$_POST variables I am looking at is undeclared.

"Notice: Undefined index: action"

I cannot openly see a way to declare the $_POST['action'] prior to
checking it since it is set by the server when the page is loaded.

Here is my actual code snippet:

if($_POST['action'] === 'login') {
//process data//
}

Will I need to learn to ignore this specific error or is there something
I am not seeing?

Thanks
Scotty
Aug 17 '08 #1
10 3240
On Sat, 16 Aug 2008 21:02:05 -0700, FutureShock <fu*********@att.net>
wrote in <Zn*****************@flpi143.ffdc.sbc.com>:

[snip]
>"Notice: Undefined index: action"

I cannot openly see a way to declare the $_POST['action'] prior to
checking it since it is set by the server when the page is loaded.

Here is my actual code snippet:

if($_POST['action'] === 'login') {
//process data//
}
Use the function isset():

$action = isset($_POST['action']) ? $_POST['action'] : '';
if ($action == 'login')
{
// process data
}

Now you have a declared variable that will have either the value
posted or a default value. You can use a different default value
depending on the type the variable holds (e.g. 0 or -1 if numeric).
--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research
Aug 17 '08 #2
Charles Calvert wrote:
On Sat, 16 Aug 2008 21:02:05 -0700, FutureShock <fu*********@att.net>
wrote in <Zn*****************@flpi143.ffdc.sbc.com>:

[snip]
>"Notice: Undefined index: action"

I cannot openly see a way to declare the $_POST['action'] prior to
checking it since it is set by the server when the page is loaded.

Here is my actual code snippet:

if($_POST['action'] === 'login') {
//process data//
}

Use the function isset():

$action = isset($_POST['action']) ? $_POST['action'] : '';
if ($action == 'login')
{
// process data
}

Now you have a declared variable that will have either the value
posted or a default value. You can use a different default value
depending on the type the variable holds (e.g. 0 or -1 if numeric).
BINGO!!!

* writes this one down in notebook.

Thanks very much for your time.

So as long as I am not trying to look at the value of it, it won't
complain about it.

Scotty

Aug 17 '08 #3
FutureShock nous disait :
if($_POST['action'] === 'login') {
//process data//
}

Will I need to learn to ignore this specific error or is there something
I am not seeing?
I'm always starting by :
if ($GLOBALS['_SERVER']['HTTP_METHOD'] == "POST") {
// Do stuff
header("Location: self.php");
exit();
}

This avoids your problem of undef item in $_POST *and also* forces the
user to use GET for viewing a form. Therefore a page refresh NEVER
re-posts stuff.

Cheers,

Nicolas

--
Nicolas - 06 20 71 62 34 - http://nicolas.bouthors.org/album/
Aug 17 '08 #4
Nicolas Bouthors wrote:
FutureShock nous disait :
>if($_POST['action'] === 'login') {
//process data//
}

Will I need to learn to ignore this specific error or is there something
I am not seeing?

I'm always starting by :
if ($GLOBALS['_SERVER']['HTTP_METHOD'] == "POST") {
// Do stuff
header("Location: self.php");
exit();
}

This avoids your problem of undef item in $_POST *and also* forces the
user to use GET for viewing a form. Therefore a page refresh NEVER
re-posts stuff.

Cheers,

Nicolas
Completely worthless. It doesn't allow you to process information
posted to the page and display them, for instance.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 17 '08 #5
Jerry Stuckle nous disait :
Completely worthless. It doesn't allow you to process information
posted to the page and display them, for instance.
Assuming the rest of the page starts with a mysql_fetch it does ;)
The intent is to always post to change data, and to always get to view
data.

My opinion anyway, glad you shared yours,

Nico
--
Nicolas - 06 20 71 62 34 - http://nicolas.bouthors.org/album/
Aug 17 '08 #6
Nicolas Bouthors wrote:
Jerry Stuckle nous disait :
>Completely worthless. It doesn't allow you to process information
posted to the page and display them, for instance.

Assuming the rest of the page starts with a mysql_fetch it does ;)
The intent is to always post to change data, and to always get to view
data.

My opinion anyway, glad you shared yours,

Nico

Not at all. For instance, how are you going to post information back to
the page, validate it, and if it's incorrect, display the data with an
error message?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 17 '08 #7
On 2008-08-17, Jerry Stuckle <js*******@attglobal.netwrote:
Nicolas Bouthors wrote:
>Jerry Stuckle nous disait :
>>Completely worthless. It doesn't allow you to process information
posted to the page and display them, for instance.

Assuming the rest of the page starts with a mysql_fetch it does ;)
The intent is to always post to change data, and to always get to view
data.

My opinion anyway, glad you shared yours,

Nico


Not at all. For instance, how are you going to post information back to
the page, validate it, and if it's incorrect, display the data with an
error message?
Save the invalid data in $_SESSION and redirect back to the original page
with $_GET["err"] set or something, instead of redirecting to the success
page.

--
Andrew Poelstra ap*******@wpsoftware.com
To email me, use the above email addresss with .com set to .net
Aug 17 '08 #8
Andrew Poelstra wrote:
On 2008-08-17, Jerry Stuckle <js*******@attglobal.netwrote:
>Nicolas Bouthors wrote:
>>Jerry Stuckle nous disait :
Completely worthless. It doesn't allow you to process information
posted to the page and display them, for instance.
Assuming the rest of the page starts with a mysql_fetch it does ;)
The intent is to always post to change data, and to always get to view
data.

My opinion anyway, glad you shared yours,

Nico

Not at all. For instance, how are you going to post information back to
the page, validate it, and if it's incorrect, display the data with an
error message?

Save the invalid data in $_SESSION and redirect back to the original page
with $_GET["err"] set or something, instead of redirecting to the success
page.
This is probably one of the worst ways to do it. Not only does it
require extra processing on both the server the the client, you now have
to keep track of additional data within the $_SESSION. Additionally,
you're doing the data entry on one page and validation on another -
which means if there is any change in the form, you have to update (at
least) two pages, instead of just one.

It means a lot of completely unnecessary work for both the computers and
the programmer.

Much cleaner is to post back to the same page, validate the data and if
it's good, do whatever you need with the data. As a final step (and
only a final step), just redirect to a "thank you" page or whatever else
is appropriate.

That way you're not doing unnecessary redirects for error handling and
you keep everything in one file. Much cleaner.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 17 '08 #9
..oO(Nicolas Bouthors)
>FutureShock nous disait :
>if($_POST['action'] === 'login') {
//process data//
}

Will I need to learn to ignore this specific error or is there something
I am not seeing?

I'm always starting by :
if ($GLOBALS['_SERVER']['HTTP_METHOD'] == "POST") {
// Do stuff
header("Location: self.php");
exit();
}

This avoids your problem of undef item in $_POST *and also* forces the
user to use GET for viewing a form. Therefore a page refresh NEVER
re-posts stuff.
Ugly, too complicated and even violating the HTTP RFC. The correct way
is to check that the POST values exist before you want to use them.

Micha
Aug 17 '08 #10
Jerry Stuckle wrote:
Andrew Poelstra wrote:
>On 2008-08-17, Jerry Stuckle <js*******@attglobal.netwrote:
>>Nicolas Bouthors wrote:
Jerry Stuckle nous disait :
Completely worthless. It doesn't allow you to process information
posted to the page and display them, for instance.
Assuming the rest of the page starts with a mysql_fetch it does ;)
The intent is to always post to change data, and to always get to view
data.

My opinion anyway, glad you shared yours,

Nico
Not at all. For instance, how are you going to post information back
to the page, validate it, and if it's incorrect, display the data
with an error message?

Save the invalid data in $_SESSION and redirect back to the original page
with $_GET["err"] set or something, instead of redirecting to the success
page.

This is probably one of the worst ways to do it. Not only does it
require extra processing on both the server the the client, you now have
to keep track of additional data within the $_SESSION. Additionally,
you're doing the data entry on one page and validation on another -
which means if there is any change in the form, you have to update (at
least) two pages, instead of just one.

It means a lot of completely unnecessary work for both the computers and
the programmer.

Much cleaner is to post back to the same page, validate the data and if
it's good, do whatever you need with the data. As a final step (and
only a final step), just redirect to a "thank you" page or whatever else
is appropriate.

That way you're not doing unnecessary redirects for error handling and
you keep everything in one file. Much cleaner.

I have found that keeping all the form entry, processing, validating and
error reporting on one page makes my job tons easier.

And as Jerry said, now you only have one page to alter if you need to
change anything.

Thanks
Scotty
Aug 18 '08 #11

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

Similar topics

6
by: jsgoodrich | last post by:
I am looking for some help if anyone can lend a hand. I have a simple php website that displays a table from my mysql database. To prep for my MCSE I moved my home server to Windows 2003...
3
by: number1yan | last post by:
Can anyone help me, i am creating a website and am using a php script that recomends the website to other people. I keep getting the same error and can not work out why. The error is: Notice:...
2
by: Reggie | last post by:
am trying to create a a upload file.am uploading files ok but i recieve this message. Notice: Undefined variable: uploaded_size in /home/fhlinux169/c/ clashoff.co.uk/user/htdocs/upload.php on...
3
by: sickboy | last post by:
$channels=$_GET; if (empty($channels)) { $channels='blank'; } changechannels($channels); $theatre=$_GET; if (empty($theatre)) { $theatre='splash'; } changetheatre($theatre); $info=$_GET; if...
5
by: Pseudonyme | last post by:
Dear All : Ever had an httpd error_log bigger than the httpd access log ? We are using Linux-Apache-Fedora-Httpd 2006 configuration. The PHP lines code that lead too tons of errors are : ...
1
by: francsutherland | last post by:
Notice: Undefined index: send in D:\Domains\workingdata.co.uk\wwwroot\contact_text.php on line 7 Hi, I've started getting this error in the contact page form of my website. The web hosting...
11
by: stealthmode666 | last post by:
New to .php so need script help. I want to know how to stop this being displayed when I click submit. Notice: Undefined index: homeowner in E:\domains\r\...\user\htdocs\...\form.php on line 166 ...
4
by: complearn | last post by:
Do you know why I got these errors for status_bar.php in some webhost, but work perfectly on other webhost? Whats wrong? here are the codes from line 18, our webhost is running php5. ...
2
by: Smellydog | last post by:
I'm having a strange issue with PHP version 5.2.8 running on Windows Vista with Lighttpd. I receive a Notice that says the following: Notice</b>: Undefined index: name in...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.