[RESOLVED] != $_POST[]: "Notice: Undefined index"
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: [RESOLVED] != $_POST[]: "Notice: Undefined index"

  1. #1
    Amusicazing! ATS16805's Avatar
    Join Date
    Aug 2005
    Location
    positively 4th street
    Posts
    532

    [RESOLVED] != $_POST[]: "Notice: Undefined index"

    i thought i knew how to deal with this notice, but i guess i'm wrong.

    i learned early on in my PHP studies a way to use IF logic (typically in the first few lines of code on a page) to test for $_POST['value'] to determine which part of a script to execute, based on the value of that $_POST data. it works very well, except if:

    a) i execute the page as a "standalone", in which case there is no $_POST data yet existing for this value

    b) the page is opened by another page OTHER THAN that which sends the $_POST data used in this IF logic comparison

    because in either of those cases, i get a
    Notice: Undefined index: 'value' in [filename] on [line name]
    because that $_POST data has not yet been defined.

    what can i do to avoid this notice? someone once suggested that i use $value = ""; to avoid that notice, but i CAN'T do $value = ''; to declare the variable because that would make NULL any value which is passed from $_POST during normal operation of this IF logic in the app.

    EDIT: i'm taking a guess, from somthing that i'm reading now-- trying to figure this out. is THIS the answer?:
    $value = True;

    i'm afraid i'm missing some critical, basic PHP functioning rules here. what does it appear that i need to learn-- so i could have figured this out on my own!? surely there's a simple solution to this. what is it, and why am i missing it?

    (okay, be nice-- please hold back the urge to say "because you're a numbskull!")
    Last edited by ATS16805; 11-21-2005 at 10:45 AM.
    #11: Thou shalt Unicode
    or try a BabelStone

  2. #2
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,907
    Check using isset() or empty() before using that variable.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  3. #3
    addicted thorpe's Avatar
    Join Date
    Aug 2004
    Location
    sydney
    Posts
    3,457
    it helps if you post the code that is giving you the grief, but in general, just check a variable is set before you try and use it.
    PHP Code:
    if (isset($_POST['value'])) {
      
    // do something with $_POST['value']


  4. #4
    Amusicazing! ATS16805's Avatar
    Join Date
    Aug 2005
    Location
    positively 4th street
    Posts
    532
    i've tried isset . why is that the answer? i'd swear it didnt' work for me!
    #11: Thou shalt Unicode
    or try a BabelStone

  5. #5
    addicted thorpe's Avatar
    Join Date
    Aug 2004
    Location
    sydney
    Posts
    3,457
    post your code!

  6. #6
    Amusicazing! ATS16805's Avatar
    Join Date
    Aug 2005
    Location
    positively 4th street
    Posts
    532
    here's the code. i was trying to spare you:
    PHP Code:
    if ($_POST['op'] != "add") {
    // IF START (A)

    // TESTING MATCH FOR HIDDEN FORM FIELD ABOVE. 
    // this tests for the FORM submit button
    // IT WILL ALWAYS FAIL FIRST TIME SO THE FORM IS IN FACT DISPLAYED
        
            // GET PARTS OF RECORDS - CREATE FUNCTION FOR RE-USING THIS SQL QUERY

        
    function getartists() {
            
    $get_artist_ids "select artist_id, artist_name from artist_info order by artist_name";
            
    $artist_id_result mysql_query($get_artist_ids) or die(mysql_error());
                return 
    $artist_id_result;
            }


    // if LOGIC - CHECK FOR EXISTENCE OF CONCERT RECORDS.
        // NO RESULTS = DISPLAY SORRY NO RECORDS.
        // RECORDS = SHOW CONCERT RECORDS IN SELECTOR FORM.
        
    $numrows getartists();

        if (
    mysql_num_rows($numrows) < 1) {
                                          
    // IF START (B)
            // NO RECORDS
            
    $display_block .= "<p><em>Sorry, no artists exist to select!</em></p>
                                <p>Please <a href=\"addartist.php\">go add the Artists first</a>, then come back and
                                add the Concert Date</p>"
    ;
        }
        
    // IF END (B)
         
    else {
         
    // ELSE START (B)
         
        
    $display_block "<h1>Add a Date:</h1>
        <form method=\"post\" action=\"
    $_SERVER[PHP_SELF]\" > 
    #11: Thou shalt Unicode
    or try a BabelStone

  7. #7
    addicted thorpe's Avatar
    Join Date
    Aug 2004
    Location
    sydney
    Posts
    3,457
    well... i dont see anywhere in that code where you check to see if $_POST['op'] has been set before you try and use it?

  8. #8
    Amusicazing! ATS16805's Avatar
    Join Date
    Aug 2005
    Location
    positively 4th street
    Posts
    532
    PHP Code:
    if ($_POST['op'] != "add") {
    // IF START (A)

    // TESTING MATCH FOR HIDDEN FORM FIELD ABOVE. 
    // this tests for the FORM submit button
    // IT WILL ALWAYS FAIL FIRST TIME SO THE FORM IS IN FACT DISPLAYED
        
            // GET PARTS OF RECORDS - CREATE FUNCTION FOR RE-USING THIS SQL QUERY

        
    function getartists() {
            
    $get_artist_ids "select artist_id, artist_name from artist_info order by artist_name";
            
    $artist_id_result mysql_query($get_artist_ids) or die(mysql_error());
                return 
    $artist_id_result;
            }


    // if LOGIC - CHECK FOR EXISTENCE OF CONCERT RECORDS.
        // NO RESULTS = DISPLAY SORRY NO RECORDS.
        // RECORDS = SHOW CONCERT RECORDS IN SELECTOR FORM.
        
    $numrows getartists();

        if (
    mysql_num_rows($numrows) < 1) {
                                          
    // IF START (B)
            // NO RECORDS
            
    $display_block .= "<p><em>Sorry, no artists exist to select!</em></p>
                                <p>Please <a href=\"addartist.php\">go add the Artists first</a>, then come back and
                                add the Concert Date</p>"
    ;
        }
        
    // IF END (B)
         
    else {
         
    // ELSE START (B)
         
        
    $display_block "<h1>Add a Date:</h1>
        <form method=\"post\" action=\"
    $_SERVER[PHP_SELF]\" >
            
            <p><strong>Headliner:</strong>
            <br />
            <select name=\"artist_id1\">
                <option value=\"\">-- Select Headliner --</option>"
    ;

        
    // RUN FUNCTION FOR OPTION VALUES PULL DOWN        
        
    $resultArtist1 getartists();
        while (
    $rowArtist mysql_fetch_array($resultArtist1)) {
                                                                
    // START WHILE (A)
            
    $id $rowArtist['artist_id'];
            
    $name $rowArtist['artist_name'];

            
    $display_block .= "<option value=\"$id\">
                    
    $name</option>
                    <input type=\"hidden\" name=\"name1\" value=\"
    $name\">";
        }
        
    // END WHILE (A)    
            
            
    $display_block .= "</select>
            
            <p><strong>First Supporting Act:</strong>
            <br />
            <select name=\"artist_id2\">
                <option value=\"\">-- Select First Support --</option>"
    ;

        
    $resultArtist2 getartists();
        while (
    $rowArtist mysql_fetch_array($resultArtist2)) {
                                                                
    // START WHILE (B)
            
    $id2 $rowArtist['artist_id'];
            
    $name2 $rowArtist['artist_name'];

            
    $display_block .= "<option value=\"$id2\">
                    
    $name2</option>
                    <input type=\"hidden\" name=\"name2\" value=\"
    $name2\">";
                    
        }    
        
    // END WHILE (B)
        
            
    $display_block .= "</select>
            
            <p><strong>Second Supporting Act:</strong>
            <br />
            <select name=\"artist_id3\">
                <option value=\"\">-- Select Second Support --</option>"
    ;
        
        
    $resultArtist3getartists();
        while (
    $rowArtist mysql_fetch_array($resultArtist3)) {
                                                                
    // START WHILE (C)
            
    $id3 $rowArtist['artist_id'];
            
    $name3 $rowArtist['artist_name'];

            
    $display_block .= "<option value=\"$id3\">
                    
    $name3</option>
                    <input type=\"hidden\" name=\"name3\" value=\"
    $name3\">";
                    
        }    
        
    // END WHILE (C)
        // AND FINISH MAKING FORM CONCATONATING INTO VARIABLE DISPLAY_BLOCK

        

            
    $display_block .= "</select>    
            
            <p><strong>Show Date:</strong>
            <br />
            <input type=\"text\" name=\"show_date\" size=\"20\" maxlength=\"25\" value=\"yyyy-mm-dd\" />
            </p>
            
            <p><strong>Show Time:</strong>
            <br />
            <input type=\"text\" name=\"show_time\" size=\"20\" maxlength=\"25\" value=\"HH:MM\" />
            </p>
            
            <p><strong>Ticket Price Date of Show:</strong>
            <br />
            <input type=\"text\" name=\"tix_dos\" size=\"20\" maxlength=\"25\" value=\"00.00\" />
            </p>
            
            <p><strong>Tickets in Advancee:</strong>
            <br />
            <input type=\"text\" name=\"tix_adv\" size=\"20\" maxlength=\"25\" value=\"$00.00\" />
            </p>
            
            
            <p><strong>All Ages?</strong>
            <br />
            
            <input type=\"radio\" name=\"age_req\" value=\"yes\" checked=\"checked\" /> yes
            <input type=\"radio\" name=\"age_req\" value=\"no\" /> no
            </p>
            
            <input type=\"hidden\" name=\"clear\" value=\"\" />
            
            <p><strong>Alcohol Served?</strong>
            <br />
            <input type=\"radio\" name=\"alcohol\" value=\"yes\" checked=\"checked\" /> yes
            <input type=\"radio\" name=\"alcohol\" value=\"no\" /> no
            </p>
            
            <p><strong>Concert Date Notes:</strong>
            <br />
            <textarea name=\"concert_notes\" cols=\"35\" rows=\"5\" wrap=\"virtual\">notes on this concert
            </textarea>
            </p>
            
            <input type=\"hidden\" name=\"op\" value=\"add\" />
            
            <p><input type=\"submit\" name=\"submit\" value=\"Add Date\" />
            </p>
            
            </form>
            <p>Back to <a href=\"control.htm\">main control</a></p>"
    ;


    // END ELSE B (OF IF B) 
        
    }
    // END IF 'A' CAUSE FORM IS DONE AND HIDDEN VALUE TESTED AGAIN    

    if ($_POST['op'] == "add") {
                                
    // IF START 'D'
        // IF INSIDE OF IF
        // CHECKS FOR FIELDS REQUIRED TO SUBMIT THE FORM. IF MISSING DATA, FORM WILL RELOAD
            
    if (($_POST['artist_id1'] == "") || ($_POST['show_date'] == "")) {
                                                                             
    // IF START 'E'
                
    header("Location: add_date.php");
                exit;
            }
            
    // IF END 'E'

        // INSERTING NEW DATA INTO TABLE -NOW-
        //    CONNECT TO DATABASE
        
    $conn mysql_connect("localhost""private""private")
            or die(
    mysql_error());
        
    mysql_select_db("cb_shows1"$conn) or die(mysql_error());

        
    // SQL IS STORED INTO VARIABLE
        // TO ADD FORM $POST DATA TO ARTIST_INFO TABLE
        
        
    $add_artist "insert into concerts values ('', '$_POST[artist_id1]', '$_POST[artist_id2]', '$_POST[artist_id3]', '$_POST[date_added]', '', '$_POST[show_date]', '$_POST[show_time]', '$_POST[tix_dos]', '$_POST[tix_adv]', '$_POST[age_req]', '$_POST[alcohol]', '$_POST[concert_notes]')"

        
    // PUT POST DATA INTO SEPARATE VARIABLES FOR SCREEN OUTPUT

        
    $artist1 $_POST['name1'];
        
    $artist2 $_POST['name'];
        
    $artist3 $_POST['name3'];
        
    $todayadd date('l dS \of F Y h:i A');
        
    $show_date $_POST['show_date'];
        
    $show_time $_POST['show_time'];
        
    $tix_dos $_POST['tix_dos'];
        
    $tid_adv $_POST['tix_adv'];
        
    $age $_POST['age_req'];
        
    $alcohol $_POST['alcohol'];
        
    $concert_notes $_POST['concert_notes']; 
    #11: Thou shalt Unicode
    or try a BabelStone

  9. #9
    Amusicazing! ATS16805's Avatar
    Join Date
    Aug 2005
    Location
    positively 4th street
    Posts
    532
    Quote Originally Posted by thorpe
    check to see if $_POST['op'] has been set before you try and use it?
    precisely-- so how is that done? can you explain ISSET to me please? i'm obviously missing something at php.net! or i was using it improperly.
    #11: Thou shalt Unicode
    or try a BabelStone

  10. #10
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,907
    PHP Code:
    if (isset($_POST['foo'])) {
        echo 
    $_POST['foo'];
    } else {
        echo 
    'foo not set';

    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  11. #11
    Amusicazing! ATS16805's Avatar
    Join Date
    Aug 2005
    Location
    positively 4th street
    Posts
    532
    okay, i guess this is the problem-- the way i see it
    Quote Originally Posted by laserlight
    PHP Code:
    if (isset($_POST['foo'])) {
        echo 
    $_POST['foo'];
    } else {
        echo 
    'foo not set';

    in the case of my code, isset would return false. so, how does that help? i already know that it's false-- it's what the logic of the IF statement is based on-- the "reason" that it works here, right? because there ISn't a value of 'op', and therefore, it must execute the appropriate code, accordign to the IF logic. yes? no?
    #11: Thou shalt Unicode
    or try a BabelStone

  12. #12
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,907
    Then change your condition to:
    isset($_POST['op']) && $_POST['op'] != "add"

    Or whatever it needs to be. Basically, dont use $_POST['op'] unless isset($_POST['op']) returns true.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  13. #13
    Amusicazing! ATS16805's Avatar
    Join Date
    Aug 2005
    Location
    positively 4th street
    Posts
    532
    cool! now, there's something that i can use!
    laserlight, can you tell me why you decided on that solution?

    aside: this code, entered into mine now results in parse error, unexpected T_FUNCTION , but i think i've left out an ELSE in there. do you see it? (this is "old" code, i'm coming back to after a break-- looks like i missed a couple things)

    EDIT: oh! wait... i have to use if (isset... [laserlight's code]) yes?
    #11: Thou shalt Unicode
    or try a BabelStone

  14. #14
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,907
    Post your new code.

    By the way, I suggest taking your getartists() out of the block.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  15. #15
    Amusicazing! ATS16805's Avatar
    Join Date
    Aug 2005
    Location
    positively 4th street
    Posts
    532
    Quote Originally Posted by laserlight
    Basically, dont use $_POST['op'] unless isset($_POST['op']) returns true.
    so, this is the basic function of ISSET? i'm still a bit confused here. hmmm. maybe it will come to me if i go back to php.net now.
    #11: Thou shalt Unicode
    or try a BabelStone

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
  •