I am trying to write a program in which the user will type a word into a search bar, submit it, and the program will search the contents of an array of files for that word and echo the contents of the files that contain the word into a text area. This is my code so far.
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Untitled 1</title>
</head>
<body>
<header>
<img src="search-star-jpg-1.jpg" alt="SearchStar Logo" />
</header>
<form action="" method="post">
<div>
<input type="text" name="search" size="25" />
<input type="submit" name="submit" value="Search" onclick="checkFiles()"/>
</div>
</form>
<?php
$matchedFiles = 0;
$files = scandir("files");
$filesContain = array();
$exclusion = file_get_contents("exclusion/exclusion.txt");
function checkFiles() {
extract($_POST);
$word = $search;
global $filesContain;
global $files;
global $matchedFiles;
for ($i = 0; $i < count($files); $i++) {
$temp = file_get_contents($files[i]);
if (strpos($temp, $word !== false)) {
$filesContain[$matchedFiles] = $files[i];
$matchedFiles++;
}
}
}
function fileContent() {
global $word;
global $filesContain;
global $files;
if (isset($word)) {
ini_set('fastcgi.impersonate', 1);
readfile($filesContain[0]);
}
}
?>
<form>
<textarea rows="30" cols="100"><?php echo fileContent(); ?></textarea>
</form>
</body>
</html>
Now what I can't figure out is how to get the word from the search bar. Right now, I'm trying to use a form to post the word to the same page, then extract the word from post and use it. However, when I type something in and submit it, nothing happens. I currently have it set to call the checkFiles() function on the click of the submit button, but I suspect this is the problem since whenever I have tried to echo the $word variable, I have gotten an uninitialized variable error, both before submitting a word, and afterward. But I don't know how else to call this function after submitting a word. Any help would be much appreciated.