Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

_Zaman's avatar

Date range get value is not working

Hi, I want to filter my table data between 2 dates. I add two DateTimePicker Button and try to get the value using dd($variable) but when I press the button it's showing nothing. I am confused button is working or nor??

<div class="col-md-4">
    <div class="input-group date form_meridian_datetime" data-date="2012-12-21T15:25:00Z">
        <input type="text" placeholder="from" name="from" id="from" size="16" class="form-control">
        <span class="input-group-btn">
            <button class="btn default date-reset" type="button">
            <i class="fa fa-times"></i>
            </button>
            <button class="btn default date-set" type="button">
            <i class="fa fa-calendar"></i>
            </button>
        </span>
    </div>
</div>
<div class="col-md-4">
    <div class="input-group date form_meridian_datetime" data-date="2012-12-21T15:25:00Z">
        <input type="text" placeholder="to" name="to" id="to" size="16" class="form-control">
        <span class="input-group-btn">
            <button class="btn default date-reset" type="button">
            <i class="fa fa-times"></i>
            </button>
            <button class="btn default date-set" type="button">
            <i class="fa fa-calendar"></i>
            </button>
            
        </span>
    </div>
    <input type="submit" name="range" id="range" value="Range" class="btn btn-success"/>
</div>

Controller

public function index(Request $request){

        
        
        $internalVisitors = InternalVisitor::all();
        $hosts = Host::all();

        // filter
        if ($request->has('from') && $request->has('to')) {
            $from = $request->get('from')->date('Y-m-d' . ' 00:00:00', time()); 
            $to = $request->get('to')->date('Y-m-d' . ' 00:00:00', time()); 

            $leads = InternalVisitor::whereBetween('signInTime', [$from, $to])->paginate(5);
            dd($leads);
        }
        

        return view('admin\internalVisitor', compact('hosts','internalVisitors'));
    }

js

<script>
$(document).ready(function(){
    $.datepicker.setDefaults({
        dateFormat: 'Y-m-d H:i:s'
    });
    $(function(){
        $("#From").datetimepicker();
        $("#to").datetimepicker();
    });
    $('#range').click(function(){
        var From = $('#From').val();
        var to = $('#to').val();
        if(From != '' && to != '')
        {
            $.ajax({
                url: '{{route('internalVisitor')}}',
                method:"POST",
                data:{From:From, to:to},
                success:function(data)
                {
                    $('#sample_3').html(data);
                }
            });
        }
        else
        {
            alert("Please Select the Date");
        }
    });
});
</script>
0 likes
4 replies
Snapey's avatar

any console errors?

open the network inspector and see if a request is sent

_Zaman's avatar

Yes. There is a console error VM11158 datatables.bootstrap.js:2 Uncaught TypeError: Cannot read property 'defaults' of undefined at VM11158 datatables.bootstrap.js:2

Cronix's avatar

for one thing, you're using a mixture of upper and lower-case names for the same thing, and they don't match.

<input type="text" placeholder="from" name="from" id="from" size="16" class="form-control">

name is "from", id is "from" - both lower-case

 $("#From").datetimepicker(); // accessing From, with upper-case F
var From = $('#From').val();  // same here
data:{From:From, to:to}, // now you send as "From" with capital F
if ($request->has('from') && $request->has('to')) { // but try to access as "from" lower case

try changing all references to "From" as "from"... be consistent... everywhere.

Please or to participate in this conversation.