Home:ALL Converter>Get multiple database queries from php to ajax success response

Get multiple database queries from php to ajax success response

Ask Time:2018-03-06T22:19:54         Author:Ralanyo

Json Formatter

I'm trying to pass two variables to my ajax success response so that i can use them in a some html to display on the page. As you can see i've tried passing them as an array but it ends up empty. How do i pass both php variables and access them in my ajax success call?

//show datpicker calendar set function on select
$( "#datepicker" ).datepicker({
    numberOfMonths: 1,
    showButtonPanel: true,
    dateFormat: "yy-mm-dd",
    onSelect: getDate
});

function getDate(response){
    var selectedDate = $('#datepicker').val();
    var values = {'selectedDate': selectedDate };

    $.ajax({
        url: '/reservations/admin/dateRequest',
        type: 'POST',
        data: values,
        dataType: 'json',
        success: function (response) {

          .....use response data here....

        } //end success
    });//end ajax call

    return false;
}

My PHP

public function differentDate() {
    //get selected date from datepicker selection   
    $selectedDate = $_POST['selectedDate'];
    //convert the selected date to carbon element
    $ConvertedSelectedDate =  new Carbon($selectedDate);
    //find day of week based on selection/conversion
    $dayOfWeek = $ConvertedSelectedDate->dayOfWeek;

    $times = ReservationTimeSlot::where('day_of_week', '=', $dayOfWeek)->get();
    $parties = Party::where('date', '=', $ConvertedSelectedDate)->get();

    $response = ['times' => $times, 'parties' => $parties];

    return $response;
}

all of this gives me empty arrays

 {"times":{},"parties":{}}

Author:Ralanyo,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49133013/get-multiple-database-queries-from-php-to-ajax-success-response
yy