As a newbie in programming I need your help in building a small app which will auto calculate age from dob in the form fields below with php
code! Is there a way to achieve that?
My html code is:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>form practice</title>
</head>
<body>
<div class="form2" id="form2" name="form2">
<form action="php/form2.php" method="post" id="Personalinfo">
<label for="fname">Name:</label>
<input type="text" id="fname" name="firstname" placeholder="Client
Name..">
<label for="lname">Lastname:</label>
<input type="text" id="lname" name="lastname" placeholder="Client
Lastname..">
<label for="dob">Birthday:</label>
<input type="text" id="dob" name="dob" placeholder="yyyy/mm/dd..">
<label for="age">Age:</label>
<input type="text" id="age" name="age" placeholder="Client Age..">
<input type="submit" name="submitForm2" value="Submit">
</form>
</div>
</body>
</html>
and my php code connecting to mysqli database:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['submitForm2']))
{
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$dob = $_POST['dob'];
$age = $_POST['age'];
$sql = "INSERT INTO info (firstname, lastname, dob, age)
VALUES ('{$firstname}', '{$lastname}', '{$dob}', '{$age}')";
if ($conn->query($sql) === TRUE)
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br />" . $conn->error;
}
}
else
{
echo "Are you sure you enter a firstname and the name of your html submit
is submitForm";
}
$conn->close();
?>
I've searched on the internet and saw many examples calculating the age from dob but I couldn't find anything related in nesting the result in a form field named age, and also keeping the data in the database.