PHP

The final client entry script

Example 8-5 shows the complete client entry script, derived from Example 6-7, that displays the previous <form> values and the error messages held in session variables.

Example 8-5. Client entry form derived from Example 6-7
<?php
  include 'db.inc';
  include 'error.inc';
  function fieldError($fieldName, $errors)
  {
     if (isset($errors[$fieldName]))
        echo
        "<font color=RED>$errors[$fieldName]</font><br>";
  }
  // Connect to a session.
  // Up to three session variables can be registered:
  // (1) $formVars - previously entered data that has
  //     failed validation
  // (2) $errors - an array of error messages, up to
  //     one per widget
  // (3) $custID - the customer ID of a customer
  //     to edit
  session_start(  );
  // $custID can also be passed as a GET parameter
  // If it is, override any session variable
  if (!empty($HTTP_GET_VARS["custID"]))
     $custID = clean($HTTP_GET_VARS["custID"], 5);
  // Has a custID been provided and are there no errors?
  // If so, retrieve the customer details for editing.
  if (!empty($custID) && empty($errors))
  {
     // Register the custID as a session variable
     if (!session_is_registered("custID"))
        session_register("custID");
     if (!($connection = @ mysql_pconnect($hostName,
                                         $username,
                                         $password)))
        die("Could not connect to database");
     if (!mysql_select_db($databaseName, $connection))
        showerror(  );
     $query = "SELECT * FROM customer
               WHERE cust_id = " . $custID;
     if (!($result = @ mysql_query($query, $connection)))
        showerror(  );
     $row = mysql_fetch_array($result);
     // Reset $formVars, since we're loading from
     // the customer table
     $formVars = array(  );
     // Load all the form variables with customer data
     $formVars["surname"] = $row["surname"];
     $formVars["firstName"] = $row["firstname"];
     $formVars["address1"] = $row["addressline1"];
     $formVars["city"] = $row["city"];
     $formVars["email"] = $row["email"];
     $formVars["dob"] = $row["birth_date"];
     $formVars["dob"] =
            substr($formVars["dob"], 8, 2) . "/" .
            substr($formVars["dob"], 5, 2) . "/" .
            substr($formVars["dob"], 0, 4);
  }
?>
<!DOCTYPE HTML PUBLIC
   "-//W3C//DTD HTML 4.0 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head><title>Customer Details</title></head>
<body bgcolor="white">
<form method="post" action="example.8-4.php">
<h1>Customer Details</h1>
<?php
  // Show meaningful instructions for UPDATE or INSERT
  if (!empty($custID))
     echo "<h3>Please amend your details below as
           required. Fields shown in
           <font color=\"red\">red</font> are
           mandatory.</h3>";
  else
     echo "<h3>Please fill in the details below to
           join. Fields shown in
           <font color=\"red\">red</font> are
           mandatory.</h3>";
?>
<table>
<col span="1" align="right">
<tr><td><font color="red">First name:</font></td>
    <td><? echo fieldError("firstName", $errors); ?>
        <input type="text" name="firstName"
           value="<? echo $formVars["firstName"]; ?>"
           size=50></td>
</tr>
<tr><td><font color="red">Surname:</font></td>
    <td><? echo fieldError("surname", $errors); ?>
        <input type="text" name="surname"
           value="<? echo $formVars["surname"]; ?>"
           size=50></td>
</tr>
<tr><td><font color="red">Address:</font></td>
    <td><? echo fieldError("address", $errors); ?>
        <input type="text" name="address1"
           value="<? echo $formVars["address1"]; ?>"
           size=50><td>
</tr>
<tr><td><font color="red">City:</font></td>
    <td><? echo fieldError("city", $errors); ?>
        <input type="text" name="city"
           value="<? echo $formVars["city"]; ?>"
           size=20><td>
</tr>
<tr><td><font color="red">Email/username:</font></td>
    <td><? echo fieldError("email", $errors); ?>
        <input type="text" name="email"
            value="<? echo $formVars["email"]; ?>"
            size=30><td>
</tr>
<tr><td>
    <font color="red">Date of birth (dd/mm/yyyy):</font>
    </td>
    <td><? echo fieldError("dob", $errors); ?>
        <input type="text" name="dob"
           value="<? echo $formVars["dob"]; ?>"
           size=10><td>
</tr>
</table><br>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>

by BrainBellupdated
Advertisement: