user3258571 user3258571 - 6 months ago 8
SQL Question

Option Menu Showing Undefined Index

Have an option menu that is driving me crazy.

<?PHP
$text=($_POST['selGrade']);
echo (!empty($text) ? $text : "ALL");
?>


The menu is fed by a table that is set to % to show all records on page load. On load the page shows "Notice: Undefined index: selGrade in..."
Selecting an option will echo correctly. Selecting All records from the option Menu will echo "%".

Here is my option menu:

<form id="formGrade" name="formGrade" method="post" action="#Grade">
Filter by grade level: <strong>
<select name="selGrade" id="selGrade"
onchange="formGrade.submit()">
<option value="%">all grade levels</option>
<?php
do {
?>
<option value="<?php echo $row_RecordsetGrade['Grade']?>"<?php

if ($varGrade_Recordset15 == $row_RecordsetGrade['Grade']) {echo 'selected';} ?>>
<?php echo $row_RecordsetGrade['Grade']?></option>
<?php
} while ($row_RecordsetGrade = mysql_fetch_assoc($RecordsetGrade));
$rows = mysql_num_rows($RecordsetGrade);
if($rows > 0) {
mysql_data_seek($RecordsetGrade, 0);
$row_RecordsetGrade = mysql_fetch_assoc($RecordsetGrade);
}
?>
</select>
</strong><a href="course_types.php">reset</a>
</form>


I just want it to echo the option menu selected. All grades (which is the default should echo "ALL" and everything else seems to be working fine. Problem seems to be the onload and when selecting All Records again. Any suggestions?

Answer

Try this change in your code

 if(!isset($_POST['selGrade']) || empty($_POST['selGrade']) || $_POST['selGrade']=="%"){
    $text = "ALL";
    } else {
    $text = $_POST['selGrade'];
    }
    echo $text;
Comments