How can I get my results from an html form to be outputted on a separate php file?

When the user creates a contact, then edits the contact that final edited information is shown in a file called “my data.txt”. After this, when I select my php file “save_contact_details.php” the details stored in the text file “my data.txt” are not retrieved. How can I retrieve them? At the moment the issue I’m having is that I see no data when I click on “save_contact_details.php” after I filled in all the contact persons information.

Feel free to watch this video I recorded to help you understand what I am talking about. https://streamable.com/rbw6p

http://formcontact.esy.es as every script works the save_contact_details.php code has to be incorrect in terms of the PHP code.

save_contact_details.php code

<html>
   <body>
      <?php
         $myFile=fopen("mydata.txt","r") or exit("Can’t open file!");
         // Write each line of text into the text file file
         	fwrite($myFile, $_POST["lastname"]."\r\n");
         	fwrite($myFile, $_POST["firstname"]."\r\n");
         	fwrite($myFile, $_POST["address01"]."\r\n");
         	fwrite($myFile, $_POST["address02"]."\r\n");
         	fwrite($myFile, $_POST["town"]."\r\n");
         	fwrite($myFile, $_POST["postcode"]."\r\n");
         	fwrite($myFile, $_POST["telephone"]."\r\n");
         	fwrite($myFile, $_POST["email"]."\r\n");
         	fclose($myFile);
         ?>
      <h1>My Contact Details</h1>
      <p>The contact details that you have submitted are shown below:</p>
      <table>
         <tr>
            <td align="right">Last name: </td>
            <td><?php echo $_POST["lastname"]; ?></td>
         </tr>
         <tr>
            <td align="right">First name: </td>
            <td><?php echo $_POST["firstname"]; ?></td>
         </tr>
         <tr>
            <td align="right">Address 01: </td>
            <td><?php echo $_POST["address01"]; ?></td>
         </tr>
         <tr>
            <td align="right">Address 02: </td>
            <td><?php echo $_POST["address02"]; ?></td>
         </tr>
         <tr>
            <td align="right">Town / city: </td>
            <td><?php echo $_POST["town"]; ?></td>
         </tr>
         <tr>
            <td align="right">Post code: </td>
            <td><?php echo $_POST["postcode"]; ?></td>
         </tr>
         <tr>
            <td align="right">Telephone: </td>
            <td><?php echo $_POST["telephone"]; ?></td>
         </tr>
         <tr>
            <td align="right">E-mail: </td>
            <td><?php echo $_POST["email"]; ?></td>
         </tr>
      </table>
   </body>
</html>

You are opening the file to read it, not write to it.

I’ll also point out, that doing a line for each isn’t the best idea. You may want to consider a csv file, each record on its own line and then you can parse it.

Sponsor our Newsletter | Privacy Policy | Terms of Service