lec-php-03
$movies = [
[
"id" => "10775",
"title" => "Infernal Affairs"
],
[
"id" => "508883",
"title" => "The Boy and the Heron"
],
[
"id" => "940721",
"title" => "Godzilla Minus One"
]
];What value does $movies[1]["title"] have?
Write a partial that would create an ordered list of ids. Assume $movies is in scope.
$books = [
9780 => [
"author" => "Ann Napolitano",
"title" => "Hello Beautiful: A Novel"
],
8950 => [
"author" => "Michael Finkel",
"title" => "The Art Thief"
],
];
$target = ???;Finish the code so that $target has the value "The Art Thief". You must use $books to do this.
Name 3 different keys in the associative array $books.
◉ Forms markup refresher
◉ What happens when we submit a form?
◉ How can we grab things from a form?
◉ How do we validate a form on the server side?
◉ How do we persist values on a form so that our users are happy?
◉ Lab-01 code observations
◉ Project admin pages layout
echo "<p>You are looking quite <span style='color: blue;'>
$randomAdjective</span>
this <span style='color: blue;'>
$greeting</span>, <span style='color: blue;'>
$name</span>.</p>";
echo "<h2>You are looking quite " .
random_adjective() . " " . "this " .
get_greetings() . "," . " " .
$name_of_person . "." . "</h1>";It's hard to read, hard to think about, hard to debug, hard to maintain, easy to make mistakes, and hides what's actually going on.
function timeOfDay() {
// OK? Or should use time_of_day?
}for ($i = 0; $i < $numImages; $++) {
// OK? Or should use $num_images?
}Be consistent. I lean toward snake_case, simply because the functions in the PHP standard library use_underscores.
function time_of_day() {
$hour = date("G");
if ($hour >= 5 && $hour <= 11) {
echo "morning";
} else if ($hour >= 12 && $hour <= 16) {
echo "afternoon";
} else {
echo "evening";
}Leave the writing to the response to the view code. Functions that echo are less reusable...and will come back to bite you once we start playing with sessions.
MDN has a lovely tutorial on Web forms - and many useful form-related reference articles as well.
<form method="post">
<ul>
<li>
<label for="name">Name:</label>
<input type="text" id="name" name="user-name" />
</li>
<li>
<label for="pwd">Password:</label>
<input type="password" id="pwd" name="pwd" />
</li>
<li>
<label for="msg">Message:</label>
<textarea id="msg" name="user-message"></textarea>
</li>
<li>
<button type="submit">Submit It!</button>
</li>
</ul>
</form>If label's for matches an input's id, what benefit do we get?
What's happening in the network tab when we land on this page?
<form method="post">
<fieldset>
<legend>Confused?</legend>
<ul>
<li>
<label for="confused-1">
<input type="radio" id="confused-1" name="confused" value="y" />
yes
</label>
</li>
<li>
<label for="confused-2">
<input type="radio" id="confused-2" name="confused" value="n" checked />
no
</label>
</li>
</ul>
</fieldset>
<p>
<button type="submit">Submit It!</button>
</p>
</form>Using fieldsets with radios/checkboxes is a good idea.
What does the checked attribute do for radios/checkboxes?
What's happening in the network tab when we land on this page?
<form method="post">
<ul>
<li>
<label for="name">Name:</label>
<input type="text" id="name" name="user-name" />
</li>
<li>
<label for="pwd">Password:</label>
<input type="password" id="pwd" name="pwd" />
</li>
<li>
<label for="msg">Message:</label>
<textarea id="msg" name="user-message"></textarea>
</li>
<li>
<button type="submit">Submit It!</button>
</li>
</ul>
</form>If we don't supply an action, the current page handles the submission.
How does an input's name attribute relate to what happens on submission?
What's happening in the network tab when we submit this form?
<form method="post">
<fieldset>
<legend>Confused?</legend>
<ul>
<li>
<label for="confused-1">
<input type="radio" id="confused-1" name="confused" value="y" />
yes
</label>
</li>
<li>
<label for="confused-2">
<input type="radio" id="confused-2" name="confused" value="n" />
no
</label>
</li>
</ul>
</fieldset>
<p>
<button type="submit">Submit It!</button>
</p>
</form>How does a radio button's value attribute relate to what happens on submission?
What's happening in the network tab when we submit this form?
<?php
if ($_SERVER['REQUEST_METHOD'] === "GET") {
// We've just arrived. Show the form...how?
} else if ($_SERVER['REQUEST_METHOD'] === "POST") {
// Someone must have submitted the form!
// Grab form values and do stuff.
}There are quite a few HTTP request methods.
We only care about GET and POST in this course.
$_SERVER is an example of a superglobal.
Notice we're NOT using an else here! Why not?
<?php
if ($_SERVER['REQUEST_METHOD'] === "GET") {
require './views/form.view.php';
} else if ($_SERVER['REQUEST_METHOD'] === "POST") {
$name = $_POST['user-name'];
$password = $_POST[???];
$message = $_POST[???];
$break = "I'm just here to be a breakpoint!";
}$__POST is another superglobal - a very important one.
Say we want to let the user know we've received their message - and if their password is too short (say 8 characters), we'll chastise them.
Because users like being chastised.
Our todo list:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Thank you!</title>
</head>
<body>
<h1>Thanks, <?= $name ?>!</h1>
<p>We received your message.</p>
<p><?= $password_warning ?></p>
</body>
</html>Notice our view is pretty darn "dumb" - not a lot of logic in here. That's a desirable trait for views!
if ($_SERVER['REQUEST_METHOD'] === "GET") {
require './views/form.view.php';
} else if ($_SERVER['REQUEST_METHOD'] === "POST") {
$name = $_POST['user-name'];
$password = $_POST['pwd'];
$message = $_POST['user-message'];
$password_warning = "";
if (strlen($password) < 8) {
$password_warning = "Your password's a bit...short.";
}
require './views/form-confirmation.view.php';
}Don't forget to bring the view in!
We need to dive into query strings more for that...and that's a topic for Wednesday.
We don't have all the pieces for that yet - and the Milestone that needs it (Milestone 3 onward) is still a ways away. Relaaaaaax.
Let's assume that we want to make sure the name field isn't empty - or only whitespace.
If the name IS empty, we want to redisplay the form with an error message.
} else if ($_SERVER['REQUEST_METHOD'] === "POST") {
$name = $_POST['user-name'];
$password = $_POST['pwd'];
$message = $_POST['user-message'];
$errors = [];
if (strlen(trim($name)) === 0) {
$errors['user-name'] = "The name can't be empty.";
}
if (empty($errors)) {
$password_warning = "";
if (strlen($password) < 8) {
$password_warning = "Your password's a bit...short.";
}
require './views/form-confirmation.view.php';
} else {
require './views/form.view.php';
}
}<li>
<label for="name">Name:</label>
<input type="text" id="name" name="user-name" />
<?php if (isset($errors['user-name'])) : ?>
<p class="error"><?= $errors['user-name'] ?></p>
<?php endif; ?>
</li>Use a conditional to add markup with the error message. Why do we need the isset?
That's reeeeeeeeally annoying, especially for cases where you've done a lot of typing.
Fortunately, there's a way to deal with this.
<li>
<label for="pwd">Password:</label>
<input type="password" id="pwd" name="pwd" value="<?= $password ?>" />
</li>Add the value attribute with the info the user had filled in.
You might be tempted to try something like this:
This doesn't work! Why?
How can we fix this?
<li>
<label for="msg">Message:</label>
<textarea id="msg" name="user-message"><?= $message ?></textarea>
</li>No value here! Just text between the tags.
Radio buttons and checkboxes are a bit different as well. Hint: use the checked attribute....