When you create a form, you should always validate particular form elements to determine if required form elements have been filled out or that the correct format is given (e.g., email address). There are a host of techniques on how to validate form elements. While minimum validation was done with the required keyword, additional validation can be done with some of the form elements.
<form action = "" method="POST">
<div class = "ui-body ui-body-a ui-corner-all">
<p><label for = "FirstName">First Name:</label>
<input name = "FirstName" type="text" required="required" id="FirstName" data-mini = "true" ></p>
<p><label for = "LastName">Last Name:</label>
<input name = "LastName" type = "text" required="required" id = "LastName" data-mini = "true" ></p>
<p><label for = "Title">Title:</label>
<input name = "Title" type ="text" required="required" id = "Title" data-mini = "true" ></p>
<p><label for = "City">City:</label>
<input name = "City" type = "text" required="required" id="City" data-mini = "true" ></p>
<p><label for = "State">State:</label>
<input name = "State" type = "text" required="required" id = "State" data-mini = "true" ></p>
<p><label for="StartDate">Start Date:</label>
<input name = "StartDate" type = "date" required="required" id = "StartDate" data-mini = "true" ></p>
<p><label for = "Note">Note:</label>
<textarea cols = "40" rows = "8" name ="Note" id = "Note" data-mini = "true" class = "define-textarea"></textarea></p>
<p><input type = "submit" name = "add_record" value = "ADD EMPLOYEE" data-inline = "true"
data-mini = "true" data-icon = "plus" data-iconpos = "left"/></p>
</div>
</form>
CODE EXPLANATION:.
- You may notice the word required used by itself in some books. To be HTML5 valid, attributes need to be in single or double quotes. While required = "required" is not nice looking, it makes the code HTML 5 valid.
It is best practice to DYNIMICALLY populate a menu if the SAME menu will be used on MULTIPLE pages. This way, if there are any changes (additions or deletions) to the menu, it can be done from one table in the database and all of the pages will reflect the changes.
<textarea cols = "40" rows = "8" name ="Note" id = "Note" data-mini = "true" class = "define-textarea"></textarea></p>
<p><label for = "Department">Department:</label>
<select name = "Department" id = "Department" data-mini = "true" data-inline = "true">
<option>Human Resource</option>
<option>IT</option>
<option>Engineering</option>
<option>Marketing</option>
<option>Manufacturing</option>
<option>Sales</option>
</select></p>
/* STEP 3: Retrieve data from Department column for combo box */
$pdo_statement = $pdo_conn->prepare( "SELECT DepartmentID, Dept FROM departments" );
$pdo_statement->execute();
$result3 = $pdo_statement->fetchAll();
?>
<!doctype html>
CODE EXPLANATION:
- Notice that there are separate names for each SQL query result (e.g., $result1, $result2, and $result3)
so that they can be used effectively later.
<p><label for = "Department">Department:</label>
<select name="Department" id="Department" data-mini="true" data-inline="true"> <!-- Loop through middle of select tag ----------------------------------------- --> <?php
foreach ( $result3 as $row ) {
?> <option><?php echo $row['Dept']; ?></option> <?php
}
?>
<!-- End of select ------------------------------------------------------------------------- --> </select></p>
CAUTION: Pay attention to the added comma after the Note statement.
/* STEP 2: Insert updated form elements to database */
if(!empty($_POST["add_record"])) {
$pdo_statement=$pdo_conn->prepare("UPDATE employees SET FirstName='" . $_POST[ 'FirstName' ] . "',
LastName='" . $_POST[ 'LastName' ]. "',
City='" . $_POST[ 'City' ]. "',
State='" . $_POST[ 'State' ]. "',
StartDate='" . $_POST[ 'StartDate' ]. "',
Note='" . $_POST[ 'Note' ]. "',
Image='" . $_POST[ 'Image' ]. "', Department=' . $_POST[ Department ]. "' WHERE ID=" . $_GET[recordID]);
$result2 = $pdo_statement->execute();
if($result2) {
header('location:master_page.php');
}
}
If you were to click the EDIT icon link on the EMPLOYEE MASTER PAGE for several of the employees, you will see that the menu on the Edit Employee Page ALWAYS reflects the same value from the Department combo box. To SYNC the recordset to the correct value from the employees table requires some additional coding.
<select name="Department" id="Department" data-mini="true" data-inline="true">
<option>Human Resource</option>
<option>IT</option>
<option>Engineering</option>
<option>Marketing</option>
<option>Manufacturing</option>
<option>Sales</option>
</select>
<select name="Department" id="Department" data-mini="true" data-inline="true">
<option>Human Resource</option>
<option>IT</option>
<option selected>Engineering</option>
<option>Marketing</option>
<option>Manufacturing</option>
<option>Sales</option>
</select>
<select name = "Department" id = "Department" data-mini = "true" data-inline = "true"><!-- Loop through middle of select tag ----------------------------------------- -->
<?php
foreach ( $DeptResult as $row ) {
?> <option><?php echo $row['Dept']; ?></option> <?php }
?>
<!-- End of select ------------------------------------------------------------------------- -->
</select></p>
<select name="Department" id="Department" data-mini="true" data-inline="true">
<!-- Loop through middle select ----------------------------------------- -->
<?php
foreach ( $result3 as $row ) {
if ( $row['Dept'] === $result[0]['Department']) {
?>
<!-- If selected ------------------------------- -->
<option selected><?php echo $row['Dept']; ?></option>
<?php
} else {
?>
<!-- If NOT selected --------------------------- -->
<option><?php echo $row['Dept']; ?></option> <?php
}
}
?>
<!-- End of select ---------------------------------------------------- -->
</select>
CODE EXPLANATION:
- Within the foreach loop, the "if" statement check each loop iteration to see if the Dept variable from the departments table MATCHES the
Department variable from the employees table and if so INJECT the selected attribute within that <option> opening tag.
<select name="Department" id="Department" data-mini="true" data-inline="true"> <!-- Loop through middle select ----------------------------------------- --> <!-- If selected ------------------------------- --> <option selected>Human Resource</option> <!-- If NOT selected --------------------------- --> <option>IT</option> <!-- If NOT selected --------------------------- --> <option>Engineering</option> <!-- If NOT selected --------------------------- --> <option>Marketing</option> <!-- If NOT selected --------------------------- --> <option>Manufacturing</option> <!-- If NOT selected --------------------------- --> <option>Sales</option> <!-- End of select ---------------------------------------------------- --> </select>
If you want to you can add the Department value to the ListView on the Employee Master Page.
CAUTION: Don't forget to include the comma after the word Image.
$sql = "SELECT ID, CONCAT(FirstName, ' ' , LastName) AS FullName, Title, Image, Department FROM employees
WHERE FirstName LIKE :keyword OR LastName LIKE :keyword OR Title LIKE :keyword ORDER BY FullName ASC";
CODE EXPLANATION:
- Notice that when you used the SELECT SQL clause, it does not matter that the column names in not in the same order
as the columns in the database. The Department column was listed last above even though it is listed eight of eleven in the database.
<p style = 'color:orange; text-transform:uppercase'><?php echo $row["Title"]; ?></p>
<p class="ui-li-aside"><strong style = "color:orange;">Dept.: <br></strong><?php echo $row["Department"]; ?></p>
Similar steps need to be done to the EDIT EMPLOYEE page to enable a user to UPDATE an image for a given employee.
<?php require 'uploadScript.php';?>
<?php
<form action = "" method="POST" enctype="multipart/form-data" data-ajax="false">