-1

I am getting following error when the html form is loaded for the very first time. Once I select the option from dropdown..all works perfectly fine.

Error I get is for th very first time: Notice: Undefined index: dlocation in C:\xampp\htdocs\ds\dsearch.php on line 48

    <?php
        $queryfilter = "SELECT distinct dlocation FROM desmit";
        $resultfilter = mysql_query($queryfilter);
        $dlocationfilter=$_POST['dlocation'];
        echo $dlocationfilter;
    ?>

     <form name='frmSearch' action='' method='post'>
        <select name="dlocation">
        <option value="All">All </option>
        <?php
        while ($line = mysql_fetch_array($resultfilter, MYSQL_ASSOC)) {
        ?>
        <option value="<?php echo $line['dlocation'];?>"> <?php echo $line['dlocation'];?> </option>

        <?php
        }
        ?>
        </select>

3 Answers3

2

I Don't know what you trying to do but use isset() function of php to take $_POST value

<?php
            $queryfilter = "SELECT distinct dlocation FROM desmit";
            $resultfilter = mysql_query($queryfilter);
            $dlocationfilter= isset($_POST['dlocation']) ? $_POST['dlocation'] : '';
            echo $dlocationfilter;
        ?>
Amit Gaud
  • 756
  • 6
  • 15
  • Thank You So So very very much Amit. My problem is solved successfully . – vikram kapoor Jun 03 '17 at 07:50
  • Great pleasure here @vikramkapoor – Amit Gaud Jun 03 '17 at 07:51
  • Hi Amit... I am facing 1 more challenge if u can help me... When I scroll to next page.. my dropdown filter value gets reset to the first OPTION of select. how can i stop this from resetting , so that the drop down value remains same on every page instead of getting reset on each page. – vikram kapoor Jun 03 '17 at 16:15
  • You have to use session for that you store dropdown value to session variable and you can access this session in any page – Amit Gaud Jun 03 '17 at 17:22
0

It say index of array not exists, so check index exists:

<?php
$queryfilter = "SELECT distinct dlocation FROM desmit";
$resultfilter = mysql_query($queryfilter);
$dlocationfilter=f(isset($_POST['dlocation']) && $_POST['dlocation']) ? $_POST['dlocation'] : null;
echo $dlocationfilter;
?>

<form name='frmSearch' action='' method='post'>
    <select name="dlocation">
        <option value="All">All </option>
        <?php
        while ($line = mysql_fetch_array($resultfilter, MYSQL_ASSOC)) {
            if(isset($line['dlocation']) && $line['dlocation']) {
            ?>
                <option value="<?php echo $line['dlocation'];?>"> <?php echo $line['dlocation'];?> </option>
            <?php
            }

        }
        ?>
    </select>
Mohammad Hamedani
  • 3,304
  • 3
  • 10
  • 22
-1

add this line to your .htaccess to hide notice:

php_flag display_errors off
aidinMC
  • 1,415
  • 3
  • 18
  • 35