Undefined index and constants question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PHPWanabe
    New Member
    • Apr 2010
    • 5

    #1

    Undefined index and constants question

    I recently started to learn PHP and a lot of this is starting to make sense, once in a while I get stump and confused at why some things just don’t work the way the book I am following works out.

    This week I started to learn about forms and how to use different methods of building them out.

    This example is from my book and even though the actions and results are what the book says they should, I get an error message that I just can’t figure it out how to fix it.

    I am using PHP 5.3.0 / apache 2.2.11 / mysql 5.1.36 on my test server

    This is my html code:

    Code:
    <html>
    <head>
    <title>Feet to Meters Comversion Tool</title>
    </head>
    <body>
    <?php
    //check to see if the form has been submitted
    $feet = $_GET['feet'];
    if ($_GET['feet'] != NULL) {
    echo "<strong>$feet </strong> feet converts to <strong>";
    echo $feet * 0.3048;
    echo "</strong> meters.<br />";
    }
    ?>
    <form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET">
    <label>Feet:
    <input type="text" name="feet" value="<?php echo $feet; ?>" />
    </label>
    <input type="submit" value="Convert!" />
    </form>
    </body>
    </html>


    When I open the form with my browser (IE, Mozilla, and Chrome) it get this error message:


    Notice: Undefined index: feet in /localhost/example2.php on line 10



    After entering a value to calculate, the form process the input and presents me the correct product. The form does what it is supposed to do and there is no problems I can see with the functionality of it. I just would like to know what the error is about and how to correct it.

    Thanks for your help,
  • guillermobytes
    New Member
    • Jan 2010
    • 77

    #2
    Originally posted by PHPWanabe
    I recently started to learn PHP and a lot of this is starting to make sense, once in a while I get stump and confused at why some things just don’t work the way the book I am following works out.

    This week I started to learn about forms and how to use different methods of building them out.

    This example is from my book and even though the actions and results are what the book says they should, I get an error message that I just can’t figure it out how to fix it.

    I am using PHP 5.3.0 / apache 2.2.11 / mysql 5.1.36 on my test server

    This is my html code:

    Code:
    <html>
    <head>
    <title>Feet to Meters Comversion Tool</title>
    </head>
    <body>
    <?php
    //check to see if the form has been submitted
    $feet = $_GET['feet'];
    if ($_GET['feet'] != NULL) {
    echo "<strong>$feet </strong> feet converts to <strong>";
    echo $feet * 0.3048;
    echo "</strong> meters.<br />";
    }
    ?>
    <form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET">
    <label>Feet:
    <input type="text" name="feet" value="<?php echo $feet; ?>" />
    </label>
    <input type="submit" value="Convert!" />
    </form>
    </body>
    </html>


    When I open the form with my browser (IE, Mozilla, and Chrome) it get this error message:


    Notice: Undefined index: feet in /localhost/example2.php on line 10



    After entering a value to calculate, the form process the input and presents me the correct product. The form does what it is supposed to do and there is no problems I can see with the functionality of it. I just would like to know what the error is about and how to correct it.

    Thanks for your help,
    the error means that you are trying to assign some key's value of some array that is not defined.
    lets say you have an array like this:
    Code:
    $array = array('a'=>'value1', 'b'=>'value2', 'c'=>'value3');
    if you try to access the first value like this:
    Code:
    $array['a'];
    everything works fine because it is defined, however if you try to assign the value of some undefined key (like lets say 'z') it will output a notice like the one you showed:
    Code:
    $feet = $array['z']; //outputs : Notice undefined index...
    to avoid this problem you should always check if the key is set before trying to access it, like this:
    Code:
    if (isset($array['z'])) {
        $zValue = $array['z'];
    } else {
        //'z' key is not defined, do something else
    }
    to solve your porblem in your script you should do this:
    Code:
    //check to see if the form has been submitted
    if (isset($_GET['feet'])) {
    $feet = $_GET['feet'];
    } else {
    $feet = 'the get variable \'feet\' was not submitted';
    }

    Comment

    • PHPWanabe
      New Member
      • Apr 2010
      • 5

      #3
      Thank you for your help. Specially for the explanation.

      I finally got the form working free of warnings and error messages.

      Thanks, appreciate the help


      This is the final code. Maybe somebody else can use this example.



      Code:
      <html>
      <head>
      <title>Feet to Meters Comversion Tool</title>
      </head>
      <body>
      <?php
      // using error reporting to clean code
      error_reporting(E_ALL);
      //check to see if the form has been submitted
      if (isset($_GET['feet'])) {
      $feet = $_GET['feet'];
      echo "<strong>$feet </strong> feet converts to <strong>";
      echo $feet * 0.3048;
      echo "</strong> meters.<br />";
      }
      else {
      // set variable to 'empty' so the input box is not populated when accessing the form for the first time
      $feet = '';
      }
      ?>
      <form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET">
      <label>Feet:
      <input type="text" name="feet" value="<?php echo $feet; ?>" />
      </label>
      <input type="submit" value="Convert!" />
      </form>
      </body>
      </html>

      Comment

      • PHPWanabe
        New Member
        • Apr 2010
        • 5

        #4
        I have one more question:

        What is the caveat of just letting the error checking ignore those warning messages. Specifically the one I was having? Is there a security risk from having that malformed form? (No, I didn’t thought about making it rime)

        The form does what it is supposed to do and it has no other problems.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          you break the programme flow. further, an undefined variable may later cause a real error.

          Comment

          Working...