SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Undefined index: action in C:\wamp\www\

    I'm just practicing some basic stuff but i don't know why i keep getting this error:

    Notice: Undefined index: action in C:\wamp\www\phpvt\lesson09\simpleform.php on line 14


    This is my form:

    PHP Code:
    <?php
            
    if($_GET['action'] == 'submit')    {
                echo((
    $_GET['txtUsername']));
            }
        
    ?>
    Code HTML4Strict:
    <form action="simpleform.php" method="get">
     
    		<fieldset>
    			<legend>Login Information</legend>
    			Username: <input type="text" name="txtUsername" maxlength="20" size="20" /><br />
    			Password: <input type="password" name="pwdPassword" maxlength="20" size="20" /><br />
    			<input type="submit" name="action" value="submit" />		
    		</fieldset>
     
    	</form>

    Both the php code and the xhtml form code above are in the same file called simpleform.php

    I really don't know why i'm getting this error.

    Any thoughts?

    Thank you.
    Jaysone

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You should not rely on reading the value of buttons. They are not always sent when a form is submitted in all circumstances, for example, if the form is submitted by pressing enter in a text field instead of clicking the button. Make the action a hidden field on the form instead.

    <input type="hidden" name="action" value="submit" />

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I notice that i don't get that error when i changed the way i get errors and notices in the php.ini file.

    I changed this:

    error_reporting = E_ALL

    to this:

    error_reporting = E_ALL & ~E_NOTICE | E_STRICT

    But just to learn more, why did i get the error?

    Was there something wrong with my code?

    Thank you.
    Jaysone

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,264
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    Some might say it's not an error, but a notice. As the PHP docs say http://us3.php.net/manual/en/errorfunc.constants.php
    Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
    they also say http://us3.php.net/manual/en/errorfu...rror-reporting
    For debugging purposes: NOTICE messages will warn you about possible bugs in your code. For example, use of unassigned values is warned. It is extremely useful to find typos and to save time for debugging. NOTICE messages will warn you about bad style.
    In any case, this is because the code is trying to get the value for $_GET['action'] to test if it equals 'submit', but the variable may or may not exist (defined vs. undefined). An easy way to get rid of the E_NOTICE error is by using isset(). Many, many, many examples of code not using isset() can be found. Either the coders don't know or don't care. But if you do care, try
    PHP Code:
    <?php
            
    if ( isset($_GET['action']) && ($_GET['action'] == 'submit') ) {
                echo 
    $_GET['txtUsername'];
            }

    ?>
    If $_GET['action'] is not set, the isset() returns FALSE and PHP doesn't continue with the if and try to get the GET variable.

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    You should not rely on reading the value of buttons. They are not always sent when a form is submitted in all circumstances, for example, if the form is submitted by pressing enter in a text field instead of clicking the button. Make the action a hidden field on the form instead.

    <input type="hidden" name="action" value="submit" />
    Oh..Ok so there may be users who might hit the submit button and also some users who might just hit enter on the keyboard instead of clicking on the input button.

    So should it be done like this?

    Code HTML4Strict:
    <form action="simpleform.php" method="get">
     
            <fieldset>
                <legend>Login Information</legend>
                Username: <input type="text" name="txtUsername" maxlength="20" size="20" /><br />
                Password: <input type="password" name="pwdPassword" maxlength="20" size="20" /><br />
                <input type="hidden" name="action" value="submit" /><!-- added this additional hidden input -->
                <input type="submit" name="action" value="submit" />       
            </fieldset>
     
        </form>

    Thank you again
    Jaysone

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Mittineague and Dan Grossman
    Jaysone


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
  •