Can anyone tell me why this wont work??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott D

    #1

    Can anyone tell me why this wont work??

    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

    #2
    Re: Can anyone tell me why this wont work??

    Rather than use
    (!($_POST['location_other ']))
    use
    (!array_key_exi sts("location_o ther", $_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.0 307091116.805f9 70@posting.goog le.com...[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]


    Comment

    • Phil Roberts

      #3
      Re: Can anyone tell me why this wont work??

      With total disregard for any kind of safety measures
      dkb1400@yahoo.c om (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($_POS T['location_other '])) rather than
      just testing for true or false.

      --
      There is no signature.....

      Comment

      • Bruno Desthuilliers

        #4
        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




        Comment

        • Paul Liversidge

          #5
          Re: Can anyone tell me why this wont work??

          dkb1400@yahoo.c om (Scott D) wrote in message news:<febdebb1. 0307091116.805f 970@posting.goo gle.com>...[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.

          Comment

          • 2metre

            #6
            Nice one. Very clean construction! (nt)


            "Paul Liversidge" <paul_liversidg e@hotmail.com> wrote in message
            news:bf26a194.0 307091835.7e0f4 381@posting.goo gle.com...
            [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]


            Comment

            Working...