0

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

In this once the submit button is clicked it retrieve the data from database. But once i enter this page Undefined index: submit is shown before i click the submit button.

<div id="title">
    <form method="get" action="<?php echo $_SERVER['PHP_SELF']?>">
        <h3>Select your product and click submit</h3><br />
        <select name="prod">
            <option value="default">Select your product</option>
            <option value="os">Operating Systems</option>
            <option value="mobile">Smart Mobiles</option>
            <option value="mobile">Computers</option>
            <option value="shirt">Shirts</option>
        </select><br /><br />
        <input type="submit" value="Submit" name="submit"/>
    </form>

The php code is:

<?php
    $submitcheck=$_GET['submit'];
    echo $submitcheck;
    if (!isset($submitcheck)) {
        echo 'Pls select and submit';
    } else {
        ....
    }

?>

Community
  • 1
  • 1
Bright
  • 63
  • 2
  • 6
  • 13

2 Answers2

7

You need to change your PHP to:

if (isset($_GET['submit']))
{
    // Form has been submitted
}
else
{
    // Form has not been submitted
}

At the moment, you're assigning a value which might not exist to $submitcheck and then checking whether it's set. You need to do it the other way round: check $_GET['submit'] is set, then assign it to a variable.

hohner
  • 11,498
  • 8
  • 49
  • 84
1

It's bacause you code is executed from top to bottom and on the top you are using $_GET['submit'] and then this key does not exist. You could do something like that:

if(array_key_exists('submit', $_GET)){
   echo $_GET['submit'];
}

or even

if (!isset($_GET['submit']))
{
    echo 'Pls select and submit';
}
else
{
 ....
}
Adrian Serafin
  • 7,665
  • 5
  • 46
  • 67