www.webdeveloper.com
Results 1 to 3 of 3

Thread: How To: Get rid of undefined index errors

  1. #1
    Join Date
    Aug 2011
    Posts
    12

    How To: Get rid of undefined index errors

    So this is one of the most common questions i see people post throughout forums. Hopefully it will be made as a sticky topic on this forum because i think its well needed. Anyways heres a few different methods for writing your post/get request without getting an undefined index error.

    Method 1
    PHP Code:
    <?php
    /**
    Checks to be sure the post array isset before actually attempting to get the value
    if it isset we get the value otherwise we return a default value of false
    **/
    $name = (isset($_POST['name'])) ? $_POST['name'] : false;
    ?>
    Method 2
    PHP Code:
    <?php
    /**
    Create a function to handle the post request and to be sure it isset
    **/
    function post($post){
    if(isset(
    $_POST[$post])){
    return 
    $_POST[$post];
    }
    }
    //Create a post request to get a form field value that has a name of name
    $name post('name');
    ?>
    Method 3 (see replies, below)
    Code:
    <?php
    //Declare the variable before trying to do a post request
    $name = "";
    $name = $_POST['name'];
    ?>
    so anyways these are just a few methods. Hope it helps people not to have to ask this question so often.
    Last edited by NogDog; 01-31-2013 at 01:53 PM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,537
    Method #3 won't help, as you'll still get the notice if $_POST['name'] is not yet defined. I usually use either the isset() or !empty() functions (depending on specific circumstances/needs) as you did in Method #1.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2011
    Posts
    12
    Quote Originally Posted by NogDog View Post
    Method #3 won't help, as you'll still get the notice if $_POST['name'] is not yet defined. I usually use either the isset() or !empty() functions (depending on specific circumstances/needs) as you did in Method #1.
    Since your a mod maybe u can edit out tht 3rd method for me if u want

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles