Creating and Editing Text Files

In the following exercise you will create a simple application that allows you to enter data into a form and upload it to a server as a text file, and use another form to view and edit the data on the server. The first form is embedded within an HTML page. Create the page using the code shown below, and save it with the filename "create_contact.html".

<html>
<body>

<h1>My Contact Details</h1>

<p>Please enter your contact details:</p>

<form action="save_contact_details.php" method="post">
  <table>
    <tr>
      <td align="right">Last name: </td>
      <td><input size=\"20\" type="text" size="20" maxlength="15" name="lastname"></td>
    </tr>
    <tr>
      <td align="right">First name: </td>
      <td><input size=\"20\" type="text" size="20" maxlength="15" name="firstname"></td>
    </tr>
    <tr>
      <td align="right">Address line 1: </td>
      <td><input size=\"30\" type="text" size="60" maxlength="50" name="address01"></td>
    </tr>
    <tr>
      <td align="right">Address line 2: </td>
      <td><input size=\"30\" type="text" size="60" maxlength="50" name="address02"></td>
    </tr>
    <tr>
      <td align="right">Town / city: </td>
      <td><input size=\"20\" type="text" size="25" maxlength="20" name="town"></td>
    </tr>
    <tr>
      <td align="right">Post code: </td>
      <td><input size=\"10\" type="text" size="12" 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" size="60" 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>

The completed data entry form is shown below.


The completed data entry form

The completed data entry form


The script that accepts the form data from "create_contact.html" will be used to create the text file "mydata.txt" and write the data into the file as a series of text strings. It also generates HTML output that repeats the form data to the screen. Create the script using the code shown below, and save your file as "save_contact_details.php".

<html>
  <body>

  <h1>My Contact Details</h1>

  <p>The contact details you 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>

  <?php
  $myFile=fopen("mydata.txt","w") 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);
  ?>

  </body>
</html>

The output from "save_contact_details.php" is shown below.


The output from "save_contact_details.php"

The output from "save_contact_details.php"


The final element of the application is a script that opens the "mydata.txt" file and reads the data into the appropriate boxes in a form ready to be edited. When the user makes changes to the data and clicks on the "Save changes" button, the amended details are passed to the "save_contact_details.php" script, which writes the new details back to the "mydata.txt" file. Create the script as shown below, and save it as "edit_contact_details.php".

<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>

The output from "edit_contact_details.php" is shown below.


The output from "edit_contact_details.php"

The output from "edit_contact_details.php"