Dynamic pages
Server-side rendering
Heroes Page
<?php
$heroes = [
[
"name" => "Bruce Wayne",
"hero" => "Batman",
"city" => "Gothan City"
],
[
"name" => "Peter Parker",
"hero" => "Homem Aranha",
"city" => "New York"
],
[
"name" => "Clark Kent",
"hero" => "Super Homem",
"city" => "Metropolis"
]
];
?>
<?php require('heroes.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Nome</th>
<th>Herói</th>
<th>Cidade</th>
</tr>
</thead>
<tbody>
<?php foreach ($heroes as $hero): ?>
<tr>
<td><?php echo implode("</td><td>", $hero); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
Heroes JSON
[
{
"name": "Bruce Wayne",
"hero": "Batman",
"city": "Gothan City"
},
{
"name": "Peter Parker",
"hero": "Homem Aranha",
"city": "New York"
},
{
"name": "Clark Kent",
"hero": "Super Homem",
"city": "Metropolis"
}
]
<?php $heroes = json_decode(file_get_contents("heroes.json"), true); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Nome</th>
<th>Herói</th>
<th>Cidade</th>
</tr>
</thead>
<tbody>
<?php foreach ($heroes as $hero): ?>
<tr>
<td><?php echo implode("</td><td>", $hero); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
http://localhost:8080/php/web/dynamic-pages/codes/heroes-json/
Hello Compact
<?php $name = $_POST['name'] ?? null; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="name">
<input type="submit" value="Olá...">
</form>
<?php if($name){ ?>
<div>
<?php echo "Olá $name"; ?>
</div>
<?php } ?>
</body>
</html>
http://localhost:8080/php/web/dynamic-pages/codes/post-hello-compact/
Layout
layout
├── index.php
├── page1.php
└── page2.php
<?php $page = $_GET['page'] ?? "page1"; ?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<nav><a href="index.php?page=page1">pagina 1</a><a href="index.php?page=page2">pagina 2</a></nav>
<div>
<?php include($page.".html"); ?>
</div>
</body>
</html>
<h1>Page 1</h1>
<h1>Page 2</h1>
Client-side rendering
Hello GET
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="">
<input type="text" name="name">
<input type="submit" value="Olá...">
</form>
<div id="hello-message"></div>
<script src="js/main.js"></script>
</body>
</html>
const name = document.querySelector('input[type=text]')
const submit = document.querySelector('input[type=submit]')
const helloMessage = document.querySelector('#hello-message')
submit.addEventListener('click', function(event) {
event.preventDefault()
const url = `../api/hello.php?name=${name.value}`
fetch(url)
.then(res => res.json())
.then(json => loadHello(json))
})
function loadHello(message) {
helloMessage.innerHTML = message.body
}
<?php
$name = $_GET['name'] ?? '';
$result = [];
$result['body'] = "Olá $name";
header("Content-type: application/json; charset=UTF-8");
header("Access-Control-Allow-Origin: *");
echo json_encode($result);
?>
http://localhost:8080/php/web/dynamic-pages/codes/get-hello-fetch/public/
Hello POST
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="">
<input type="text" name="name">
<input type="submit" value="Olá...">
</form>
<div id="hello-message"></div>
<script src="js/main.js"></script>
</body>
</html>
const name = document.querySelector('input[type=text]')
const submit = document.querySelector('input[type=submit]')
const helloMessage = document.querySelector('#hello-message')
submit.addEventListener('click', function(event) {
event.preventDefault()
const url = `../api/hello.php`
const config = {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: `name=${name.value}`
}
fetch(url, config)
.then(res => res.json())
.then(json => loadHello(json))
})
function loadHello(message) {
helloMessage.innerHTML = message.body
}
<?php
$name = $_POST['name'] ?? '';
$result = [];
$result['body'] = "Olá $name";
header("Content-type: application/json; charset=UTF-8");
header("Access-Control-Allow-Origin: *");
echo json_encode($result);
?>
http://localhost:8080/php/web/dynamic-pages/codes/post-hello-fetch/public/