Home > Enterprise >  Display info from text file based on name submited in a form?
Display info from text file based on name submited in a form?

Time:05-04

I’m not sure how to accomplish this.

In the index page I have a form (as seen below) where someone can type in Biden, Trump, Obama, etc for example and then press submit and on the next page it will display information about the presidents. I have the index page working as what was not hard at all, however on the next page witch is “find.php” is where I am having some issues getting anything to work.

<form action="find.php" method="POST">
<input type="text" name="potus">
<input type="submit">
</form>

On this page as seen below is the find.php page where I have entered this code in:

<?php
$file = fopen("works.txt", "r") or exit("unable to open file!");
//output a line of the file until the end is reached
while(!feof($file))
{
//will return each line with a break
echo fgets($file). '<br />';
}
fclose($file);
?>

I know what if I add this <?php echo $_POST["potus"]; ?> on the next page it should display what the user entered on the first page witch was “Biden”, but when I add this code to the code that I already have, the page is blank. The code that I have pulls the data from the text file.

<?php
$file = fopen("<?php echo $_POST["potus"]; ?>.txt", "r") or exit("unable to open file!");
//output a line of the file until the end is reached
while(!feof($file))
{
//will return each line with a break
echo fgets($file). '<br />';
}
fclose($file);
?>

CodePudding user response:

Instead of

$file = fopen("<?php echo $_POST["potus"]; ?>.txt", "r") or exit("unable to open file!");

try

$file = fopen($_POST["potus"] . ".txt", "r") or exit("unable to open file!");
  • Related