作为编程的新手,我需要您的帮助来构建一个小应用程序,该应用程序将使用php
代码自动从以下表单域中计算dob的年龄!有没有办法实现这一点?使用javascript自动计算年龄dob使用javascript
我的HTML代码是:
<!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>
和我的PHP代码连接到数据库的mysqli的:
<?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();
?>
我已经在互联网上搜索,看到很多例子,从出生日期计算年龄,但我在将结果嵌套在名为age的表单字段中并且将数据保留在数据库中时找不到任何相关内容。
感谢伊万卡! –