By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,625 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,625 IT Pros & Developers. It's quick & easy.

Can anyone tell me why this wont work??

P: n/a
I am trying to check and see if a field is posted or not, if not
posted then assign $location which is a session variable to
$location_other. If it is posted then just assign it to
$location_other

I keep getting "Notice: Undefined index: location_other" referring to
(!($_POST['location_other'))
if (!($_POST['location_other'])) {
$location_other = $location;
}
else
if ($_POST['location_other'])
$location_other = $_POST['location_other'];

THANKS!!!
-Scott
Jul 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Rather than use
(!($_POST['location_other']))
use
(!array_key_exists("location_other", $_POST))
which checks whether there is a variable named location_other in the _POST
array.

Mike
PS haven't checked this code, but should be OK
"Scott D" <dk*****@yahoo.com> wrote in message
news:fe*************************@posting.google.co m...
I am trying to check and see if a field is posted or not, if not
posted then assign $location which is a session variable to
$location_other. If it is posted then just assign it to
$location_other

I keep getting "Notice: Undefined index: location_other" referring to
(!($_POST['location_other'))
if (!($_POST['location_other'])) {
$location_other = $location;
}
else
if ($_POST['location_other'])
$location_other = $_POST['location_other'];

THANKS!!!
-Scott

Jul 16 '05 #2

P: n/a
With total disregard for any kind of safety measures
dk*****@yahoo.com (Scott D) leapt forth and uttered:
I am trying to check and see if a field is posted or not, if not
posted then assign $location which is a session variable to
$location_other. If it is posted then just assign it to
$location_other

I keep getting "Notice: Undefined index: location_other"
referring to (!($_POST['location_other'))
if (!($_POST['location_other'])) {
$location_other = $location;
}
else
if ($_POST['location_other'])
$location_other = $_POST['location_other'];

THANKS!!!
-Scott


Its because you're using an array key ($_POST['location_other'])
which hasn't been declared.

The easiest way to get around this is to use
error_reporting(E_ALL ^ E_NOTICE), this will disregard 'Notice'
errors but keep all others.

Either that or use if(!empty($_POST['location_other'])) rather than
just testing for true or false.

--
There is no signature.....
Jul 16 '05 #3

P: n/a
Joshua Ghiloni wrote:
Scott D wrote:
I am trying to check and see if a field is posted or not, if not
posted then assign $location which is a session variable to
$location_other. If it is posted then just assign it to
$location_other

I keep getting "Notice: Undefined index: location_other" referring to
(!($_POST['location_other'))
if (!($_POST['location_other'])) {
$location_other = $location;
}
else if ($_POST['location_other'])
$location_other = $_POST['location_other'];

THANKS!!!
-Scott

Read your errors :) That's saying that the index doesn't exist, and
you're checking to see if it's true. change your condition to

if (isset($_POST['location_other']))

and all should be well with the world.


I would add that your code is somewhat messy... Your conditional goes
like this :

if false then
doSomething()
else if true then
doSomethingElse
end if

don't you think the second test is useless ? And given the variables
names, it would also seems more readable to first handle the true part.

Try :

if (isset($_POST['location_other']))
{
$location_other = $_POST['location_other']
}
else
{
$location_other = $location;
}

Clearer, isn't it ?-) Reading this, you see more clearly that $location
is a default value for $location_other...

Bruno


Jul 16 '05 #4

P: n/a
dk*****@yahoo.com (Scott D) wrote in message news:<fe*************************@posting.google.c om>...
I keep getting "Notice: Undefined index: location_other" referring to
(!($_POST['location_other'))
if (!($_POST['location_other'])) {
$location_other = $location;
}
else
if ($_POST['location_other'])
$location_other = $_POST['location_other'];


I tend to use the following construct at the top of my pages
instead...

$location_other = isset ($_POST['location_other']) ?
$_POST['location_other'] : $location;

It's only one line and makes it easier when you've got 20 form
variables coming in. It also forces default values to be set, which
can be useful during the next stage, which is when I validate the form
variables before doing any processing upon them.
Jul 16 '05 #5

P: n/a

"Paul Liversidge" <pa*************@hotmail.com> wrote in message
news:bf**************************@posting.google.c om...

I tend to use the following construct at the top of my pages
instead...

$location_other = isset ($_POST['location_other']) ?
$_POST['location_other'] : $location;

It's only one line and makes it easier when you've got 20 form
variables coming in. It also forces default values to be set, which
can be useful during the next stage, which is when I validate the form
variables before doing any processing upon them.

Jul 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.