Skip to content

Commit a8f5f3c

Browse files
committed
Finished Chapter 14 exercise 2
1 parent 9070f7c commit a8f5f3c

File tree

4 files changed

+205
-0
lines changed

4 files changed

+205
-0
lines changed

.devcontainer/devcontainer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2+
// README at: https://github.com/devcontainers/templates/tree/main/src/php
3+
{
4+
"name": "PHP",
5+
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
6+
"image": "mcr.microsoft.com/devcontainers/php:1-8.2-bullseye",
7+
8+
// Features to add to the dev container. More info: https://containers.dev/features.
9+
// "features": {},
10+
11+
// Configure tool-specific properties.
12+
// "customizations": {},
13+
14+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
15+
"forwardPorts": [8080]
16+
17+
// Use 'postCreateCommand' to run commands after the container is created.
18+
// "postCreateCommand": "sudo chmod a+x \"$(pwd)\" && sudo rm -rf /var/www/html && sudo ln -s \"$(pwd)\" /var/www/html"
19+
20+
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
21+
// "remoteUser": "root"
22+
}

.github/dependabot.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for more information:
4+
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5+
# https://containers.dev/guide/dependabot
6+
7+
version: 2
8+
updates:
9+
- package-ecosystem: "devcontainers"
10+
directory: "/"
11+
schedule:
12+
interval: weekly

ch14_formvalidator.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
if (!isset($_GET["username"]) && !isset( $_GET["email"])) {
3+
echo "PHP is working correctly. Congratulations!";
4+
return;
5+
}
6+
7+
header("Content-Type: application/json");
8+
header("Cache-Control: no-cache");
9+
10+
// type for returned data
11+
class ReturnObject {
12+
public $available;
13+
public $searchTerm;
14+
public function __construct() {
15+
16+
}
17+
}
18+
19+
// get query string as associative array
20+
parse_str($_SERVER['QUERY_STRING'], $query);
21+
22+
$values = array("username" => array("jmcpeak","pwilton"),
23+
"email" => array("someone@zyx.com", "someone@xyz.com"));
24+
25+
function processRequest() {
26+
global $query, $values;
27+
28+
$obj = new ReturnObject();
29+
$obj->available = TRUE;
30+
31+
foreach($query as $key => $value) {
32+
$arrValues = $values[$key];
33+
34+
$obj->searchTerm = $value;
35+
36+
for ($ii = 0; $ii < count($arrValues); $ii++) {
37+
if (strtolower($arrValues[$ii]) == strtolower($value)) {
38+
$obj->available = FALSE;
39+
break;
40+
}
41+
}
42+
}
43+
44+
return $obj;
45+
}
46+
47+
echo json_encode(processRequest());
48+
49+
?>

ch14exercise2.html

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<!DOCTYPE html>
2+
3+
<html lang="en">
4+
<head>
5+
<title>Chapter 14: Example 1</title>
6+
<style>
7+
.fieldname {
8+
text-align: right;
9+
}
10+
11+
.submit {
12+
text-align: right;
13+
}
14+
</style>
15+
</head>
16+
<body>
17+
<form id="whole">
18+
<table>
19+
<tr>
20+
<td class="fieldname">
21+
Username:
22+
</td>
23+
<td>
24+
<input type="text" id="username" />
25+
</td>
26+
</tr>
27+
<tr>
28+
<td class="fieldname">
29+
Email:
30+
</td>
31+
<td>
32+
<input type="text" id="email" />
33+
</td>
34+
</tr>
35+
<tr>
36+
<td class="fieldname">
37+
Password:
38+
</td>
39+
<td>
40+
<input type="text" id="password" />
41+
</td>
42+
<td />
43+
</tr>
44+
<tr>
45+
<td class="fieldname">
46+
Verify Password:
47+
</td>
48+
<td>
49+
<input type="text" id="password2" />
50+
</td>
51+
<td />
52+
</tr>
53+
<tr>
54+
<td colspan="2" class="submit">
55+
<input type="submit" value="Submit" id="Sub" />
56+
</td>
57+
<td />
58+
</tr>
59+
</table>
60+
</form>
61+
<script src="ch14exercise1.js"></script>
62+
<script>
63+
function checkUsername(e) {
64+
e.preventDefault();
65+
66+
var userValue = document.getElementById("username").value;
67+
68+
if (!userValue) {
69+
alert("Please enter a user name to check!");
70+
return;
71+
}
72+
73+
var url = "ch14_formvalidator.php?username=" + userValue;
74+
75+
var request = new HttpRequest(url, handleUsername, true);
76+
request.send();
77+
}
78+
79+
function checkEmail() {
80+
e.preventDefault();
81+
82+
var emailValue = document.getElementById("email").value;
83+
84+
if (!emailValue) {
85+
alert("Please enter an email address to check!");
86+
return;
87+
}
88+
89+
var url = "ch14_formvalidator.php?email=" + emailValue;
90+
91+
var request = new HttpRequest(url, handleEmail, true);
92+
request.send();
93+
}
94+
95+
function handleUsername(responseText) {
96+
var response = JSON.parse(responseText);
97+
98+
if (response.available) {
99+
alert(response.searchTerm + " is available!");
100+
checkEmail();
101+
} else {
102+
alert("We're sorry, but " + response.searchTerm + " is not available.");
103+
return;
104+
}
105+
}
106+
107+
function handleEmail(responseText) {
108+
var response = JSON.parse(responseText);
109+
110+
if (response.available) {
111+
alert(response.searchTerm + " is available!");
112+
document.getElementById("Sub").submit();
113+
} else {
114+
alert("We're sorry, but " + response.searchTerm + " is not available.");
115+
return;
116+
}
117+
}
118+
document.getElementById("Sub").addEventListener("click", checkUsername);
119+
</script>
120+
</body>
121+
122+
</html>

0 commit comments

Comments
 (0)