In my html I have a search function that when the submit button is clicked a PHP function is ran that is supposed to filter out rows that do not contain the searched word and only the rows in the HTML table that contained the searched word display on the page. My HTML table is being generated using PHP which is reading books from a .txt file. My code brings back the rows from the text file instead of filtering the table results. Any help please?
HTML
<form action="search.php" method="POST">
<p>Enter Word to Search</p>
<p>
<input type="text" name="search_term"/>
<input type="submit" value="Search"/>
</form>
PHP
$search_term = $_POST['search_term'];
foreach($books as $book){
$book_formatted = str_replace('|', '', $book);
$pos = stripos($book_formatted, $search_term);
if($pos === false){
print "Not found";
}else{
$book_formatted = substr($book_formatted, 0, $pos);
$book_formatted = trim($book_formatted);
$pos2 = stripos($book_formatted, $search_term);
if($pos2 === false){
print "Not found in the list";
}else{
print "Titles that match: ". $book_formatted;
}
}
}