Connecting Tech Pros Worldwide Forums | Help | Site Map

Can anyone tell me why this wont work??

Scott D
Guest
 
Posts: n/a
#1: Jul 16 '05
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
2metre
Guest
 
Posts: n/a
#2: Jul 16 '05

re: Can anyone tell me why this wont work??


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" <dkb1400@yahoo.com> wrote in message
news:febdebb1.0307091116.805f970@posting.google.co m...[color=blue]
> 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[/color]


Phil Roberts
Guest
 
Posts: n/a
#3: Jul 16 '05

re: Can anyone tell me why this wont work??


With total disregard for any kind of safety measures
dkb1400@yahoo.com (Scott D) leapt forth and uttered:
[color=blue]
> 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
>[/color]

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.....
Bruno Desthuilliers
Guest
 
Posts: n/a
#4: Jul 16 '05

re: Can anyone tell me why this wont work??


Joshua Ghiloni wrote:[color=blue]
> Scott D wrote:
>[color=green]
>> 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[/color]
>
>
> 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.[/color]

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




Paul Liversidge
Guest
 
Posts: n/a
#5: Jul 16 '05

re: Can anyone tell me why this wont work??


dkb1400@yahoo.com (Scott D) wrote in message news:<febdebb1.0307091116.805f970@posting.google.c om>...[color=blue]
> 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'];[/color]

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.
2metre
Guest
 
Posts: n/a
#6: Jul 16 '05

re: Can anyone tell me why this wont work??



"Paul Liversidge" <paul_liversidge@hotmail.com> wrote in message
news:bf26a194.0307091835.7e0f4381@posting.google.c om...
[color=blue]
>
> 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.[/color]


Closed Thread