PHP: $_POST

Please Enter Your Name and Age



The code used for index.php is below:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../styles.css">
    <title>PHP: $_POST</title>
</head>
<body>
<h1 class="lesson-h1">PHP: $_POST</h1>
<h3>Please Enter Your Name and Age</h3>
<?php
    $name = $_POST['name'];
    $age = $_POST['age'];
  if(isset($name) && isset($age)){
    if(!$name && !$age){
        echo "<p>Error: You must enter your name and age to continue.</p>";
      }
      else if(!$name){
          echo "<p>Error: You must enter your name to continue.</p>";
      }
      else if(!$age){
        echo "<p>Error: You must enter your age to continue.</p>";
      }
      else {
        post($name, $age);
      }
    } 

function post($n, $a){
    echo "<p>Your name is <strong>$n</strong> and you are <strong>$a</strong> years old.</p>";
    echo "<p>POST Request Submitted: => Name: <strong>$_POST[name]</strong> | Age: <strong>$_POST[age]</strong></p>";
}
?>
<form action="#" method="POST">
    <label for="name">Name:
      <input type="text" name="name" placeholder="Enter Your Name">
    </label>
    <label for="name">Age: 
      <input type="number" name="age" placeholder="Enter Your Age">
    </label>
    <input type="submit" value="Submit">
</form>
<hr>
<?php
  include('../show_code.php');
  show_code('index.php');
?>
</body>
</html>