SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict agentforte's Avatar
    Join Date
    May 2007
    Location
    Toronto, ON, Canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question notice errors.. first Undefined index, then Undefined variable

    Hello,

    I get the error:
    Notice: Undefined index: other

    the line with this error has the code:
    $a = $_POST['other'];


    I tried to fix the code by doing this:
    if (isset($_POST['other'])){
    $a = $_POST['other'];
    }


    but this just gave me a new error:
    Notice: Undefined variable: a

    I tried to define the variable:
    if (isset($_POST['other'])){
    var $a;
    $a = $_POST['other'];
    }


    but then a fatal error occurs (unexpected T_VAR )

    Can anyone suggest how I get rid of the 'undefined index' error properly (rather than just hiding it) so that new errors don't keep popping up?

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Whistler, Canada
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(isset($_POST['other'])) {
      
    $a $_POST['other'];

    That piece of code works fine. But the problem probably lies somewhere else in your script. I assume that in the next part of your script you are trying to use $a, but do mind that $a doesn't exist if $_POST['other'] has not been set. So in your next part where you're trying to use $a either add isset($a) or put $a = ''; before your isset($_POST['other'] part.

    Like this:
    PHP Code:
    $a '';

    if(isset(
    $_POST['other'])) {
      
    $a $_POST['other'];

    or
    PHP Code:
    if(isset($_POST['other'])) {
     
    $a $_POST['other'];
    }

    if(isset(
    $a)) {
      
    // do your stuff

    Hope this makes sense.

    Cheers,
    Pepe

  3. #3
    SitePoint Enthusiast SecondV's Avatar
    Join Date
    Jan 2006
    Location
    Kentucky
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, nvmd. SP died on me, IP-Dope beat me..

    However, you could also:
    Code PHP:
    $a = (isset($_POST['other'])) ? $_POST['other'] : '';

  4. #4
    SitePoint Addict agentforte's Avatar
    Join Date
    May 2007
    Location
    Toronto, ON, Canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that worked.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •