Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!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
- $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();
- ?>
- SELECT name, birth, CURDATE(),
- TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age
- FROM pet;
- $now = new DateTime();
- // Note your SQL field should be a `DATE` column
- // and formatted YYYY-MM-DD
- // assumed in this example $sqlData['dob'] = "2015-01-10";
- $birthday = new DateTime($sqlData['dob']);
- $age = $now->diff($birthday);
- //
- print "person is ".$age['y']." years old.";
- DateInterval Object
- (
- [y] => 2
- [m] => 6
- [d] => 23
- [h] => 5
- [i] => 34
- [s] => 11
- [days] => 935
- )
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>form practice</title>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
- <script>
- $(document).ready(function(){
- $("#dob").change(function(){
- var value = $("#dob").val();
- var dob = new Date(value);
- var today = new Date();
- var age = Math.floor((today-dob) / (365.25 * 24 * 60 * 60 * 1000));
- if(isNaN(age)) {
- // will set 0 when value will be NaN
- age=0;
- }
- else{
- age=age;
- }
- $('#age').val(age);
- });
- });
- </script>
- </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>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement