Physics & Computer Science > CP363 : Home > Implementation Information
Version: 2020-02-06 11:02

CP363 : Forms

The examples on this page assumes the use of the class and connection defined at the top of the Selecting Data page.


Accessing Form Data

The following HTML code:

<form name="nameForm" action="members.php" method="post">
  <table border="0">
    <tr>
      <td>
        <label for="last_name">Surname: </label>
      </td>
      <td>
        <input name="last_name" type="text" size="25" />
      </td>
    </tr>
    <tr>
      <td>
        <label for="first_name">First Name: </label>
      </td>
      <td>
        <input name="first_name" type="text" size="25" />
      </td>
    </tr>
    <tr>
      <td>
        <label for="institution">Institution: </label>
      </td>
      <td>
        <input name="institution" type="text" size="40" />
      </td>
    </tr>    
  </table>
  <p style="clear: both;">
    <input name="byName" type="submit" value="Search" />
  </p>
</form>

produces this form:

The line:

<form name="nameForm" action="members.php" method="post">

defines a form named nameForm that sends its data to the server page members.php using the post method. The name is not absolutely required, but it is useful when there are multiple forms on a page or you are using Javascripts that access form elements.

The line:

<input name="last_name" type="text" size="25" />

defines a single-line text input box named last_name that is 25 characters wide. (It has no maximum length defined - that would be done with the maxlength attribute - so the box is allowed to overflow.)

The other input text boxes are similar - what is important is that their names distinguish them. Each text box must have a unique name attribute.

The lines:

    <input name="byName" type="submit" value="Search" /> 

define the form submission button. When this button is pushed, the contents of the form are sent to the members.php server page.

The label tags associate text with a matching input field. Clicking on the label Surname puts the cursor into the last_name text field. This is a user interface issue and does not have anything to do with PHP processing of the form.


The $_POST Variable

When a form is submitted to a server page all of the form's named input fields are placed into an associative array named $_POST. The name of each field is used as the name of an associated element. For example, if the previous form is filled out as follows:

Then the resulting values in the $_POST variable are:

$_POST = Array
(
    [last_name] => Kilgour
    [first_name] => 
    [institution] => Wilf
    [byName] => Search
)

These values can then be processed as required, as in this example:

$sql = 'SELECT * FROM member WHERE last_name LIKE ? AND first_name LIKE ? AND institution LIKE ?';
$stmt = $this->conn->prepare( $sql );
$i = 1;
$stmt->bindValue( $i++, "{$_POST['last_name']}%" ); 
$stmt->bindValue( $i++, "{$_POST['first_name']}%" ); 
$stmt->bindValue( $i++, "%{$_POST['institution']}%" );
...