How to use Datepicker in PHP?

Apr 01, 2022 . Admin



Hello dev,

I am going to explain you How to use Datepicker in PHP. You will learn How to make a nice "date picker" in PHP?. In side this article we will see How To Use Bootstrap Datepicker in PHP & MySQL.

This article will give you simple example of How To Use Bootstrap Datepicker in PHP . We will use get simple How to Add Datepicker in PHP Form with PHP date picker class.

I will give you simple Example of How to use Datepicker in PHP.

So, let's see bellow solution:

connection.php
<?php 
    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "aatman";

    $conn = new mysqli($servername,$username,$password,$dbname);

    if($conn->connect_error){
        die ('connection faild:'.$conn->connect_error);
    }
?>
index.php
<?php
    session_start();
    if(isset($_SESSION["msg"]) && !empty($_SESSION["msg"]))
        {
            $msg=$_SESSION["msg"];
            echo $msg;
            unset($_SESSION['msg']);
            session_destroy();
        }
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <title>How to use Datepicker in PHP</title>
    <style type="text/css">
        .error
        {
            color: red;
        }
    </style>
</head>
<body class="">
    <div class="container mt-5">
        <?php 

            $error = $_SESSION['errors'];
            extract($error);
            session_destroy();

        ?>
        <div class="card">
            <div class="card-header">
                <h1 class="text-center">How to use Datepicker in PHP</h1>
            </div>
            <div class="card-body">
                <form action="process.php" method="post">
                    
                    <div class="row my-2">
                        <div class="col-md-6">
                            <label for="fname">First Name :</label>
                            <span class="error">*</span>
                            <input type="text" name="first_name" id="first_name" class="form-control" placeholder="Please Enter First Name">
                            <span class="error"><?php echo $first_nameErr;?></span>
                        </div>
                        <div class="col-md-6">
                            <label for="lname">Last Name :</label>
                            <span class="error">*</span>
                            <input type="text" name="last_name" id="last_name" class="form-control" placeholder="Please Enter Last Name">
                            <span class="error"><?php echo $last_nameErr; ?></span>
                        </div>
                    </div>
                    
                    <div class="row my-2">
                        <div class="col-md-6">
                            <label for="email">Email :</label>
                            <input type="email" name="email" id="email" class="form-control" placeholder="Please Enter email" >
                        </div>
                        <div class="col-md-6">
                            <label for="admission date ">Admission Date :</label>
                            <span class="error">*</span><br>
                            <input type="date" name="admission_date" id="admission date" class="form-control">
                            <span class="error"><?php echo $admission_dateErr;?></span>
                        </div>
                    </div>
                    
                    <div class="row my-2">
                        <div class="col-md-12">
                            <label for="address">Address :</label>
                            <span class="error">*</span><br>
                            <textarea name="address" id="address" rows="6" cols="40" class="form-control" placeholder="Please Enter address"></textarea>
                            <span class="error"><?php echo $addressErr;?></span>
                        </div>
                    </div>
            </div>

            <div class="card-footer text-center">
                <input type="submit" name="save" class="btn btn-primary">
                </form>
            </div>
        </div>
    </div>
</body>
</html>


process.php
<?php

    session_start();

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

    extract($_POST); 
    $error = array();

        if (empty($first_name)) {
            $error['first_nameErr'] = "first name is required!";
        }
        if (empty($last_name)) {
            $error['last_nameErr'] = "last name is required!";
        }
        if (empty($admission_date)) {
            $error['admission_dateErr'] = "admission date is required!";
        }
        
        if (!empty($error)) {
            $_SESSION['errors'] = $error;
            $_SESSION['post'] = $_POST;
            header('location:index.php');
        }
        if (empty($error)){
            
            include "connection.php";
            
            $sql = "INSERT INTO students (first_name,last_name,email,address,admission_date)
                    VALUES('$first_name','$last_name','$email','$address','$admission_date')"; 

            if ($conn->query($sql)===TRUE) {
                $_SESSION['msg'] = "<div class='alert alert-success' role='alert'>Data Insert Successfully</div>";
                header('location:index.php');
            }else{
                $_SESSION['msg'] = "<div class='alert alert-danger' role='alert'>Error: ".$sql."<br>".$conn->error."</div>";
                header('location:index.php');
            }

            $conn->close();

        }else{
            header('location:index.php');
        }
    }
?>
Output:

It will help you...
#PHP