STAY INFORMED
following content serves as a personal note and may lack complete accuracy or certainty.

Minimal-Mistakes instruction
Useful vscode Shortcut Keys
Unix Commands
npm Commands
Vim Commands
Git Note
Useful Figma Shortcut Keys

1 minute read

create form handling person’s data

create person class

<?php

class Person{

    private String $firstName;
    private String $lastName;
    private String $email;

    // Getters
    public function getFirstName() : String { return $this->firstName; }
    public function getlastName() : String { return $this->lastName; }
    public function getEmail() : String { return $this->email; }


    // Setters
    public function setFirstName(String $fName) { $this->firstName = $fName; }
    public function setLastName(String $lName) { $this->lastName = $lName; }
    public function setEmail(String $email) { $this->email = $email; }
}

?>

create page class

In this class, html code will be needed to display the page in website. I imported code from bootstrap which is front-end framework developed by Twitter. Here is a simple code displaying form.

<?php
class Page {
    private String $title = "set this title";

    static function header(){ ?>

    <!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap demo</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
  </head>
  <body>
    <div class="container">

<?php    }
static function footer(){ ?>
</div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
  </body>
</html>
<?php }
static function welcomeMessage(){
    echo "<h1>Welcome to the world Wide Web.</h1>";
}

static function registrationForm(){ ?>
<form ACTION="" method="POST">
  <div class="mb-3">
    <label for="exampleInputEmail1" class="form-label">Email address</label>
    <input type="email" class="form-control" name="email" aria-describedby="emailHelp" value="tongsik98@gmail.com">
    <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
  </div>
  <div class="mb-3">
    <label for="exampleInputPassword1" class="form-label">First name</label>
    <input type="text" name="firstName" class="form-control" id="exampleInputPassword1" value="Henry">
  </div>
  <div class="mb-3">
    <label for="exampleInputPassword1" class="form-label">Last name</label>
    <input type="text" name="lastName" class="form-control" id="exampleInputPassword1" value="Chung">
  </div>
  <div class="mb-3 form-check">
    <input type="checkbox" class="form-check-input" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">Check me out</label>
  </div>
  <input type="submit" class="btn btn-primary"></input>
</form>
<?php }

public static function showRegistractionData(Person $p){?>
<table class="table table-striped">
    <thead>
        <tr>
        <th>Item</th>
        <th>Value</th>
        </tr>
</thead>
<tbody>
    <tr><td>First Name</td><td><?php echo $p->getFirstName(); ?></td>
    <td>Last Name</td><td><?php echo $p->getFirstName(); ?></td>
    <td>Email</td><td><?php echo $p->getFirstName(); ?></td>
</tbody>
</tr>
</table>

<?php }
}

?>

call page class

<?php
require_once("inc/Utilities/Page.class.php");
require_once("inc/Entities/Person.class.php");
Page::header();
Page::welcomeMessage();

// check if there was post data, if not then show the form.
if (empty($_POST) || !isset($_POST)){

    Page::registrationForm();
}
// otherwise process the form.
else{
    var_dump($_POST);
    // Take form data

    // Sanitize it

    // Create a Person object
    $p = new Person();
    $p->setFirstName($_POST["firstName"]);
    $p->setLastName($_POST["lastName"]);
    $p->setEmail($_POST["email"]);

    // Show the details of the person registering.
    Page::showRegistractionData($p);
}


Page::footer();

// xampp - start - localhost/week4Demo
?>

To see the website, put the folder you made in cdrive/xampp/htdocs and you need to activate xampp. Once you activate it, go to ‘localhost/yourFolderName’ so that you will see the website page.

Tags:

Categories:

Updated: