Date change and Time filter appointment in mysql php

This tutorial is going to show you how to SELECT data from a MySQL database and filter selected date to time so, below code available and with example. You may also like How to Search Record from database using Codeigniter and Live Search in php with Ajax and Mysql.

Code

HTML Code

<div class="col-md-12">
    <label class="col-sm-2 label-on-left" style="padding-right: 0px;">Follow up</label> 
    <div class="col-md-4">
        <div class="form-group label-floating is-empty" style="margin-top: -10px;">
            <label class="control-label"></label>
            <input type="date" name="followup_date" value="<? echo $candidateinfo['followup_date']; ?>" placeholder="" class="form-control followup_date  check_time_followup_date" placeholder="Date of Birth">
        </div>
    </div>
    <label class="col-sm-1 label-on-left">Time </label> 
    <div class="col-md-5">
        <div class="form-group" style="margin-top: -10px;">

            <select name="followup_time" class="form-control followup_time get_followup_time" >
                <option value="">Select Time</option>
                <option value="9:00"  <? echo ($candidateinfo['followup_time'] == '9:00' ? 'selected' : '' ); ?> >9:00</option>
                <option value="9:30"  <? echo ($candidateinfo['followup_time'] == '9:30' ? 'selected' : '' ); ?> >9:30</option>
                <option value="10:00" <? echo ($candidateinfo['followup_time'] == '10:00' ? 'selected' : '' ); ?> >10:00</option>
                <option value="10:30" <? echo ($candidateinfo['followup_time'] == '10:30' ? 'selected' : '' ); ?> >10:30</option>
                <option value="11:00" <? echo ($candidateinfo['followup_time'] == '11:00' ? 'selected' : '' ); ?> >11:00</option>
                <option value="11:30" <? echo ($candidateinfo['followup_time'] == '11:30' ? 'selected' : '' ); ?> >11:30</option>
                <option value="12:00" <? echo ($candidateinfo['followup_time'] == '12:00' ? 'selected' : '' ); ?> >12:00</option>
                <option value="12:30" <? echo ($candidateinfo['followup_time'] == '12:30' ? 'selected' : '' ); ?> >12:30</option>
                <option value="13:00" <? echo ($candidateinfo['followup_time'] == '13:00' ? 'selected' : '' ); ?> >13:00</option>
                <option value="13:30" <? echo ($candidateinfo['followup_time'] == '13:30' ? 'selected' : '' ); ?> >13:30</option>
                <option value="14:00" <? echo ($candidateinfo['followup_time'] == '14:00' ? 'selected' : '' ); ?> >14:00</option>
                <option value="14:30" <? echo ($candidateinfo['followup_time'] == '14:30' ? 'selected' : '' ); ?> >14:30</option>
                <option value="15:00" <? echo ($candidateinfo['followup_time'] == '15:00' ? 'selected' : '' ); ?> >15:00</option>
                <option value="15:30" <? echo ($candidateinfo['followup_time'] == '15:30' ? 'selected' : '' ); ?> >15:30</option>
                <option value="16:00" <? echo ($candidateinfo['followup_time'] == '16:00' ? 'selected' : '' ); ?> >16:00</option>
                <option value="16:30" <? echo ($candidateinfo['followup_time'] == '16:30' ? 'selected' : '' ); ?> >16:30</option>
                <option value="17:00" <? echo ($candidateinfo['followup_time'] == '17:00' ? 'selected' : '' ); ?> >17:00</option>
                <option value="17:30" <? echo ($candidateinfo['followup_time'] == '17:30' ? 'selected' : '' ); ?> >17:30</option>
                <option value="18:00" <? echo ($candidateinfo['followup_time'] == '18:00' ? 'selected' : '' ); ?> >18:00</option>
                <option value="18:30" <? echo ($candidateinfo['followup_time'] == '18:30' ? 'selected' : '' ); ?> >18:30</option>
                <option value="19:00" <? echo ($candidateinfo['followup_time'] == '19:00' ? 'selected' : '' ); ?> >19:00</option>
                <option value="19:30" <? echo ($candidateinfo['followup_time'] == '19:30' ? 'selected' : '' ); ?> >19:30</option>
                <option value="20:00" <? echo ($candidateinfo['followup_time'] == '20:00' ? 'selected' : '' ); ?> >20:00</option>
                <option value="20:30" <? echo ($candidateinfo['followup_time'] == '20:30' ? 'selected' : '' ); ?> >20:30</option>
                <option value="21:00" <? echo ($candidateinfo['followup_time'] == '21:00' ? 'selected' : '' ); ?> >21:00</option>
                <option value="21:30" <? echo ($candidateinfo['followup_time'] == '21:30' ? 'selected' : '' ); ?> >21:30</option>
                <option value="22:00" <? echo ($candidateinfo['followup_time'] == '22:00' ? 'selected' : '' ); ?> >22:00</option>
                <option value="22:30" <? echo ($candidateinfo['followup_time'] == '22:30' ? 'selected' : '' ); ?> >22:30</option>
            </select>
        </div>
    </div>
</div> 

jQuery Code

$('.check_time_followup_date').change(function(){
    var check_date = $(this).val();
    $.ajax({
        type:'POST',
        url: "ajax_candidatequestions.php?check_time_followup_date=true",
        data: {check_date: check_date},
        dataType: 'JSON',
        success:function(data){
            $('.get_followup_time').html(data.success);

        }
    });
});

PHP Code (Call using Ajax)

$array1 = array('9:00','9:30','10:00','10:30','11:00','11:30','12:00','12:30','13:00','13:30','14:00','14:30','15:00','15:30','16:00','16:30','17:00'
        ,'17:30','18:00','18:30','19:00','19:30','20:00','20:30','21:00','21:30','22:00','22:30');
    
    $array2 = array();
    $secondarray2 = array();
    $check_date = $_POST['check_date'];
    $sql        = mysqli_query($conn,"SELECT followup_time FROM `wo_users_personaldata` WHERE followup_date = '".$check_date."' ");
    while ($row = mysqli_fetch_assoc($sql))
    {
        array_push($array2,$row['followup_time']);
    }
    
    $result = array_diff($array1,$array2);
    $html = '';
    $html .= '<option value="">Select Time</option>';
    foreach ($result as $value)
    {
        $html .='<option value="'.$value.'">'.$value.'</option>';
    }
    $json['success'] = $html;

Leave a Reply

Your email address will not be published. Required fields are marked *