Search code examples
phpsearch-engine

PHP search engine Notice: Undefined index: category


Hi guys I have made a script with some help who browse in a category Example: We have next categorie PCgames, All and Documents. X user is searching in PCgames for Y game. And the script is saerching in PCgames ategory for Y game and display the all results. But when the user click on search button the server display next error Notice: Undefined index: category in search.php on line 5 HTML code

    <form action="search.php" method="POST">
<p><br />
<input name="q" type="hidden" />
<font color="black">Hunt Data:</font>
<input type="text" style="width: 180px" name="qfront" id="name" />
<select name="category">
<option value="0" id="all">All</option>
<option value="1" id="PCgames">PC-Games</option>
<option value="2" id="Console">Console</option>
<option value="3" id="Movies">Movies</option>
<option value="4" id="Music">Music</option>
<option value="5" id="XXX">XXX</option>
<option value="6" id="Windows">Windows</option>
<option value="7" id="Linux">Linux</option>
<option value="8" id="Software">Software</option>
<option value="9" id="Documents">Documents</option>
</select>
<input type="submit" value="Search" />
</p>
</form>

search.php file

    <?php
//define each directory here, the index starts with 0 == all and so on.
$categorydir = array('/Category/All/', '/Category/PCGames/', '/Category/Documents/');
//if option selected and its valid number
if($_POST['category'] && ctype_digit($_POST['category'])){    //line 5
 if(array_key_exists($_POST['category'], $categorydir) && is_dir($categorydir[$_POST['category']])){
   $handle = opendir($categorydir[$_POST['category']]);
 }else{
   echo 'target directory not found';
 }
}else{
 //please enter an option
}
?>

Some help for Notice: Undefined index: category in search.php on line 5 ?


Solution

  • if(isset($_POST['category']) {
     if(ctype_digit($_POST['category']) && isset($categorydir[$_POST['category']])){
    
     }
    }
    

    and your array

     $categorydir = array('/Category/All/', '/Category/PCGames/', '/Category/Documents/');
    

    have 3 entries, though your form have 10 cats.