Jump to content
liamdoch

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

Recommended Posts

Hi, 

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

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>


edit_contact_details.php code

    <html>
        <body>
            <?php
            $myFile=fopen("mydata.txt","r") or exit("Can’t open file!");
            // read each line of text from the text file
            $lastname = fgets($myFile);
            $firstname = fgets($myFile);
            $address01 = fgets($myFile);
            $address02 = fgets($myFile);
            $town = fgets($myFile);
            $postcode = fgets($myFile);
            $telephone = fgets($myFile);
            $email = fgets($myFile);
            fclose($myFile);
    ?>
    <h1>My Contact Details</h1>
      <p>
        The contact details on file are as shown below.<br>
        Edit the data and save your changes to file.
      </p>
      <form action="save_contact_details.php" method="post">
      <table>
        <tr>
          <td align="right">Last name: </td><td>
          <?php echo "<input size=\"20\" type=\"text\" name=\"lastname\" value=\"$lastname\">"?>
          </td>
        </tr>
        <tr>
          <td align="right">First name: </td><td>
          <?php echo "<input size=\"20\" type=\"text\" name=\"firstname\" value=\"$firstname\">"?>
        </tr>
        <tr>
          <td align="right">Address 01: </td><td>
          <?php echo "<input size=\"30\" type=\"text\" name=\"address01\" value=\"$address01\">"?>
        </td>
        </tr>
        <tr>
          <td align="right">Address 02: </td><td>
                    <?php echo "<input size=\"30\" type=\"text\" name=\"address02\" value=\"$address02\">"?>
          </td>
        </tr>
        <tr>
          <td align="right">Town / city: </td><td>
          <?php echo "<input size=\"20\" type=\"text\" name=\"town\" value=\"$town\">"?>
        </td>
        </tr>

        <tr>
          <td align="right">Post code: </td><td>
          <?php echo "<input size=\"10\" type=\"text\" name=\"postcode\" value=\"$postcode\">"?>
        </td>
        </tr>
        <tr>
          <td align="right">Telephone: </td><td>
          <?php echo "<input size=\"15\" type=\"text\" name=\"telephone\" value=\"$telephone\">"?>
          </td>
        </tr>
        <tr>
          <td align="right">E-mail: </td><td>
          <?php echo "<input size=\"50\" type=\"text\" name=\"email\" value=\"$email\">"?>
          </td>
        </tr>
        <tr>
          <td> </td>
          <td colspan="2" align="left"><input type="submit" value="Save Changes"></td>
        </tr>
      </table>
      </form>
      </body>
    </html>

create contact html code

    <html>
    <body>
    <h1>The contact details</h1>
    <p>Please enter your contact details:</p>
    <form action="save_contact_details.php" method="post">
      <table>
        <tr>
          <td align="right">First name: </td>
          <td><input size="20" type="text"  maxlength="15" name="firstname"></td>
        </tr>
         <tr>
          <td align="right">Last name: </td>
          <td><input size="20" type="text"  maxlength="15" name="lastname"></td>
        </tr>
        <tr>
          <td align="right">Address line 1: </td>
          <td><input size="30" type="text"  maxlength="50" name="address01"></td>
    </tr>
     <tr>
          <td align="right">Address line 2: </td>
          <td><input size="30" type="text"  maxlength="50" name="address02"></td>
        </tr>
        <tr>
          <td align="right">Town / city: </td>
          <td><input size="20" type="text"  maxlength="20" name="town"></td>
        </tr>
        <tr>
          <td align="right">Post code: </td>
          <td><input size="10" type="text"  maxlength="10" name="postcode"></td>
        </tr>
        <tr>
          <td align="right">Telephone: </td>
          <td><input size="15" type="text" size="20" maxlength="15" name="telephone"></td>
        </tr>
        <tr>
          <td align="right">E-mail: </td>
          <td><input size="50" type="text"  maxlength="50" name="email"></td>
        </tr>
        <tr>
          <td> </td>
          <td colspan="2" align="left"><input type="submit" value="Submit"></td>
        </tr>
      </table>
    </form>
    </body>
    </html>

 

Share this post


Link to post
Share on other sites

Unless this is just to learn the basics of file reading and writing do not store your data in a text file. At minimum use SQLite.

Share this post


Link to post
Share on other sites

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

10 minutes ago, benanamen said:

Unless this is just to learn the basics of file reading and writing do not store your data in a text file. At minimum use SQLite.

In relation to your post, I'm just a beginner and this is for a task assigned to me. Thanks for the advice, I'll bare that in mind for the future. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.