Let's spice up our application by adding a form to add a new employee.
When we create forms, we'll use a HTTP POST method to send our form data. This has implications for how we get the form data in PHP.
What are forms exactly? A HTML form is a tag that allows us to enclose inputs to send to PHP.
<form action="index.php" method="post"></form>
Any inputs between the form tags will be sent to our index.php controller when we submit the form. Let's expand the form example by adding some inputs. These inputs will be a hidden input to help guide our controller, two text inputs for our first and last names, and a submit button. The submit button is a special input that makes the form finally push data to our controller.
Our form looks like the following:
<form action="index.php" method="post">
<input type="hidden" name="do" value="saveNewEmployee">
<input type="text" name="firstName" placeholder="first name">
<input type="text" name="lastName" placeholder="last name">
<input type="submit" value="Save">
</form>
Let's put this into our view.
This code should be put into indexviews.php in your webroot directory.
<?php
namespace IndexViews;
class IndexViews {
// $employees is a list of firstName and lastName
static public function index($employees) {
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Employees</title>
</head>
<body>
<?php
// echo the results to the browser
foreach ($employees as $res) {
echo "<div>{$res["lastName"]}, {$res["firstName"]}</div>\n";
}
?>
<form action="index.php" method="post">
<input type="hidden" name="do" value="saveNewEmployee">
<input type="text" name="firstName" placeholder="first name">
<input type="text" name="lastName" placeholder="last name">
<input type="submit" value="Save">
</form>
</body>
</html>
<?php
}
}
Okay, how do we access the form data in our PHP code? We use the PHP special superglobal variable, $_POST.
In the above example, you saw <input type="hidden" name="do" value="saveNewEmployee">. When we click the submit button, $_POST["do"] will equal "saveNewEmployee". We're going to use this to determine what class methods to execute. Let's change our controller to respond to this:
This code should be put into index.php in your webroot directory.
<?php
require_once "indexmodels.php";
use \IndexModels\IndexModels;
require_once "indexviews.php";
use \IndexViews\IndexViews;
if ( isset($_POST["do"]) == true) {
if ($_POST["do"] == "saveNewEmployee") {
IndexModels::saveNewEmployee($_POST["lastName"], $_POST["firstName"]);
// after we've saved, forward the user back to our default screen.
// we do this in case the user hits F5 or tries to go back
header("Location: /");
exit;
}
} else {
// show the default screen, which is a list of employees
$results = IndexModels::index();
IndexViews::index($results);
}
Let's look at the logic behind this. The first if checks to see if anything was POSTed. If we don't have anything POSTed, then just drop through to the else block, which shows our list of employees. This is our default screen. If something is POSTed, then we check to see what we do. If do equals saveNewEmployee, then we save the new employee to the database.
Now, let's update our Model and add our saveNewEmployee method.
This code should be put into indexmodels.php in your webroot directory.
<?php
namespace IndexModels;
require_once "db.php";
use \database\db;
class IndexModels {
static public function index() {
$db = new db;
return $db->query("select lastName, firstName from names")->fetchAll();
}
static public function saveNewEmployee($lastName, $firstName) {
$db = new db;
$db->query(
"insert into names set lastName=:lastName, firstName=:firstName",
[ ":lastName"=>$lastName, ":firstName"=>$firstName, ]
);
}
}