if (!$_POST['submit']) giving error

Discussion in 'PHP' started by mksharma, Nov 19, 2007.

  1. #1
    when i run index.php in localhost giving below error :

    Notice: Undefined index: submit in C:\Program Files\EasyPHP 2.0b1\www\index.php on line 8


    when i click on button "select" also giving error about authorization

    i have attached the error screens as jpg files.


    <html>
    <head>
    </head>
    <body>

    <?php

    if (!$_POST['submit'])
    {

    ?>
    Select from the items below: <br />
    <form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
    <select name="options[]" multiple>
    <option value="power steering">Power steering</option>
    <option value="rear wiper">Rear windshield wiper</option>
    <option value="cd changer">6 CD changer</option>
    <option value="fog lamps">Fog lamps</option>
    <option value="central locking">Central locking</option>
    <option value="onboard navigation">Computer-based navigation</option>
    </select>
    <input type="submit" name="submit" value="Select">
    </form>
    <?php
    }
    else
    {
    if (is_array($_POST['options']))
    {
    echo 'Here is your selection: <br />';
    foreach ($_POST['options'] as $o)
    {
    echo "<i>$o</i><br />";
    }
    }
    else
    {
    echo 'Nothing selected';
    }
    }
    ?>
    </body>
    </html>

    warm regards,
    mk.
     

    Attached Files:

    mksharma, Nov 19, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    This is lazy coding. It should be:

    
    if (!isset($_POST['submit']))
    
    PHP:
    Or:
    
    if (!empty($_POST['submit']))
    
    PHP:
    (Both usually work, but it depends on the case which is most appropriate)

    However, this means your error reporting is not set to it's default. Open your php.ini file and change the value in error_reporting to:
    
    error_reporting  =  E_ALL & ~E_NOTICE
    
    Code (markup):
    Usually these notices are not shown by default, but it's a good practice to code in a way that they don't occur, because it slows down your performance.
     
    nico_swd, Nov 19, 2007 IP
  3. kreoton

    kreoton Peon

    Messages:
    229
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <?=$_SERVER['PHP_SELF']?>
    PHP:
    change to
    <?php echo $_SERVER['PHP_SELF'];?>
    PHP:
    I see that on your host short php tags are disabled
     
    kreoton, Nov 19, 2007 IP
  4. mksharma

    mksharma Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thank you very much.
    your code : working perfect.

    if (!isset($_POST['submit']))

    "<?php echo $_SERVER['PHP_SELF'];?>"

    i am new in PHP.
    i have started learning PHP today only.

    warm regards,
    mk.
     
    mksharma, Nov 19, 2007 IP