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

TheDoc's avatar

Fetching Data to JS & AJAX via PHP (Laravel Script)

In my Project the user is able to book resources. When creating a new booking, a start and an until Date are selected via Datepickers. In those Datepickers I want the dates where the resource is booked to be blocked.

The selection of a resource is done via a select (dropdown). I am catching the Select-Change event via AJAX and that Part is Working. The only problem is that my php script is calles but is returning no Data.

For disabling the Dates within the datepicker i thought using the jquery datepicker whould be an option, but i have no idea how to replace my current date picker with an JQuery Datepicker.

The view where the user is able to create a new Booking is looking like this:

@extends('layouts.app')
@section('content')
    <script
            src="https://code.jquery.com/jquery-3.3.1.min.js"
            integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
            crossorigin="anonymous"></script>
    <script type="text/javascript">
        $(document).ready(function()
        {
            $('#House_id').on('change',function(){
                var HouseID = $(this).val();
                if(HouseID)
                {
                    alert(HouseID);
                    $.ajax({ type:'GET', url:'GetAJAXdata.php', data:{House_id: HouseID}, success:function(data)
                        {
                            alert("Hello?");
                            alert(data.result);
                            $("#From").attr("disabledDates", data.result);
                            $("#Until").attr("disabledDates", data.result);
                        }
                    });
                }
                else{
                    //Nope.
                }
            });
        }); </script>
    <h1>Create Season</h1>
    {!! Form::open(['action' => 'BookingController@store', 'method' => 'POST']) !!}
    <div class="form-group">
        {{ Form::label('SeasonName','Season Name') }}
        {{ Form::text('SeasonName', '', ['class' => 'form-control', 'placeholder' => 'Season Name'] )  }}
    </div>
    <div class="form-group">
        {{ Form::label('From','From') }}
        {!! Form::date('From', \Carbon\Carbon::now()) !!} {{--In here dates should get disabled according to the select down below--}}
    </div>
    <div class="form-group">
        {{ Form::label('Until','Until') }}
        {!! Form::date('Until', \Carbon\Carbon::now()) !!} {{--In here dates should get disabled according to the select down below--}}
    </div>
    <div class="form-group">
        {{ Form::label('Price','Price') }}
        {!! Form::number('Price',null,['class' => 'form-control','step'=>'any']) !!}
    </div>
    <div class="form-group">
        {{ Form::label('House_id','House') }}
        {!! Form::select('House_id', $houses, null , ['placeholder' => 'Pick a house...',]) !!}{{--This select should change the disabled dates of the datepickers above.--}}
    </div>
    {{ Form::submit('Submit',['class' => 'btn btn-success']) }}
    {!! Form::close() !!}
@endsection

The PHP FIle which is Providing the Data is looking like this:

<?php
use App\Http\Controllers\HelperFunctions;
use App\House;


if(isset($_POST["House_id"])){
        
        $house = House::find($_POST["House_id"]); //This '1' should get replaced by the id selected in House_id
        $home = HelperFunctions::GetBlockedTimes($house);
        $getit = '[';
        if($home == null){
            echo "ERROR!!!";}
        else {
            echo $getit;
        }
    }
    if(!empty($_POST["House_id"])){
     echo 'wut?';
    }

The Helper Function that is called is returning the right values (as an array) if it is fed the right way. I have tried that by Calling it directly from the form. But I don't have an idea how to do that resposivly to the Select.

<?php
namespace App\Http\Controllers;
use App\House;
use Illuminate\Http\Request;
use DB;
use App\Quotation;
use DateInterval;
use DatePeriod;
use DateIntercal;
class HelperFunctions extends Controller
{
    public static function GetBlockedTimes(House $house)
    {
        $Bookings = DB::table('bookings')->where([
                                                     ['House_id', $house->id],
                                                 ])->get();
        $BlockedTimes = [];
        foreach($Bookings as $booking) {
            for($i = strtotime($booking->From); $i <= strtotime($booking->Until); $i += 86400) {
                array_push($BlockedTimes, date('Y-m-d',$i));
            }
        }
        return $BlockedTimes;
    }
}
0 likes
1 reply

Please or to participate in this conversation.