persist in database sample form inputs and show the list of previously entered prompts when displaying form's result

pull/6/head
maxime.batista 2 years ago
parent b1da73b743
commit bd8d8a3f61

@ -11,6 +11,8 @@ export function renderView(Component: FunctionComponent, args: {}) {
document.getElementById('root') as HTMLElement document.getElementById('root') as HTMLElement
); );
console.log(args)
root.render( root.render(
<React.StrictMode> <React.StrictMode>
<Component {...args}/> <Component {...args}/>

@ -1,12 +1,15 @@
import ReactDOM from "react-dom/client";
import React from "react";
export default function DisplayResults({password, username}: any) { export default function DisplayResults({results}: { results: { name: string, description: string }[] }) {
const list = results.map(({name, description}) =>
<div>
<p>username: {name}</p>
<p>description: {description}</p>
</div>
)
return ( return (
<div> <div>
<p>username: {username}</p> {list}
<p>password: {password}</p>
</div> </div>
) )
} }

@ -5,12 +5,12 @@ export default function SampleForm() {
return ( return (
<div> <div>
<h1>Hello, this is a sample form made in react !</h1> <h1>Hello, this is a sample form made in react !</h1>
<form action="result" method="POST"> <form action="submit" method="POST">
<label>your name: </label> <label>your name: </label>
<input type="text" id="name" name="username"/> <input type="text" id="name" name="name"/>
<label>your password: </label> <label>a little description about yourself: </label>
<input type="password" id="password" name="password"/> <input type="text" id="password" name="description"/>
<input type="submit" value="click me!"/> <input type="submit" value="click me to submit!"/>
</form> </form>
</div> </div>
) )

@ -5,6 +5,7 @@ require "../config.php";
require "../sql/database.php"; require "../sql/database.php";
use App\Controller\SampleFormController; use App\Controller\SampleFormController;
use App\Gateway\FormResultGateway;
/** /**
* relative path of the index.php's directory from the server's document root. * relative path of the index.php's directory from the server's document root.
@ -28,9 +29,9 @@ $pdo = get_database();
$router = new AltoRouter(); $router = new AltoRouter();
$router->setBasePath($basePath); $router->setBasePath($basePath);
$sampleFormController = new SampleFormController(); $sampleFormController = new SampleFormController(new FormResultGateway($pdo));
$router->map("GET", "/", fn() => $sampleFormController->displayForm()); $router->map("GET", "/", fn() => $sampleFormController->displayForm());
$router->map("POST", "/result", fn() => $sampleFormController->displayResults($_POST)); $router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST));
$match = $router->match(); $match = $router->match();

@ -3,13 +3,28 @@
namespace App\Controller; namespace App\Controller;
require_once __DIR__ . "/../react-display.php"; require_once __DIR__ . "/../react-display.php";
use App\Gateway\FormResultGateway;
class SampleFormController { class SampleFormController {
private FormResultGateway $gateway;
/**
* @param FormResultGateway $gateway
*/
public function __construct(FormResultGateway $gateway)
{
$this->gateway = $gateway;
}
public function displayForm() { public function displayForm() {
send_react_front("views/SampleForm.tsx", []); send_react_front("views/SampleForm.tsx", []);
} }
public function displayResults(array $request) { public function submitForm(array $request) {
send_react_front("views/DisplayResults.tsx", $request); $this->gateway->insert($request["name"], $request["description"]);
$results = ["results" => $this->gateway->listResults()];
send_react_front("views/DisplayResults.tsx", $results);
} }
} }

@ -0,0 +1,34 @@
<?php
namespace App\Gateway;
use PDO;
use PDOStatement;
/**
* A sample gateway, that stores the sample form's result.
*/
class FormResultGateway {
private PDOStatement $insertion_stmnt;
private PDOStatement $list_stmnt;
public function __construct(PDO $pdo)
{
$this->insertion_stmnt = $pdo->prepare("INSERT INTO FormEntries VALUES (:name, :description)");
$this->list_stmnt = $pdo->prepare("SELECT * FROM FormEntries");
}
function insert(string $username, string $description) {
$this->insertion_stmnt->bindValue(":name", $username, PDO::PARAM_STR);
$this->insertion_stmnt->bindValue(":description", $description, PDO::PARAM_STR);
$this->insertion_stmnt->execute();
}
function listResults(): array {
$this->list_stmnt->execute();
return $this->list_stmnt->fetchAll();
}
}
Loading…
Cancel
Save