0

I am currently working with three pages of code, 1 HTML and 2 PHP. When a user submits their first name, last name and age it should display on the first PHP page and outputs a string thanking them for entering their info then simultaneously the HTML form data should be 1)Appended to a text file on my hard drive and 2)Outputted to an HTML table on the browser in a 3rd PHP page. I am having trouble with outputting the user data to the 3rd PHP page. I also do not receive any errors in my code which makes it difficult to pinpoint the issue. Why is my code not appending the data to the HTML table in the third PHP page?

HTML

<form action="page2.php" method="post" name="form">
    <p>Enter your info</p>

    <p>First Name:<br>
    <input type="text" name="firstname" id="firstname">
    </p>

    <p>Last Name:<br>
    <input type="text" name="lastname" id="lastname">
    </p>

    <p>Age:<br>
    <input type="text" name="age" id="age" size="3">
    </p>

PHP PAGE 2

if(isset($_POST['submit'])){
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age= $_POST['age'];}

$filename ='files/addusers.txt';
 $fp = fopen($filename, 'a');
      $text = $firstname. '|' . $lastname. '|' . $age. '|' . "\n";
      fwrite($fp, $text);
      fclose($fp); 

PHP/HTML Page 3

 if(isset($_POST['submit'])){
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age= $_POST['age'];

$filename ='files/addusers.txt';

?>

<table border = '1'>
    <tr>
        <th>Last Name</th>
        <th>First Name</th>
        <th>Age</th>
    </tr>

<?php

/*Insert User Info to HTML table*/
$output_results = "";
$cntr = 0;

$fp = fopen($filename, 'r');


while(true){
   $line = fgets($fp);

   if (feof($fp))
   {
       break;
   }

   $cntr++;

list($lastname, $firstname, $email, $bYear, $city) = explode('|', $line);



 $output_results .= "<tr>";
       $output_results .= "<td>".$lastname."</td>";
       $output_results .= "<td>".$firstname."</td>";
       $output_results .= "<td>". $age."</td>";
      $output_results .= "</tr>\n";  



  }


fclose($fp);
print $output_results;
}//END OF ISSET
?>
            </table>
CodeConnoisseur
  • 1,452
  • 4
  • 21
  • 49

1 Answers1

1

I don't know how you send the user to the third page. But I think the problem is in the first line of the third page. The user doesn't use the POST method.

You probably need to remove the if, or actually the first 4 lines of code and of course the closing if } tag.

Timo002
  • 3,138
  • 4
  • 40
  • 65
  • You are awesome, immediately solved the issue. For learning purposes, I should only use the $_POST for storing user input into a variable one time and then it should work on all connecting PHP pages? Thank you again Bud!!! – CodeConnoisseur Aug 29 '18 at 13:08
  • 1
    @WCA, No. $_POST only works whey you submitted a form with the `method="post"`. The $_POST variable is only at that moment filled with the variables in your form. If you want to use variables across multiple pages, you should use the $_SESSION variable. https://secure.php.net/manual/en/function.session-start.php – Timo002 Aug 29 '18 at 13:26