繁体   English   中英

从php获取多个数据库查询到ajax成功响应

[英]Get multiple database queries from php to ajax success response

我试图将两个变量传递给我的ajax成功响应,以便可以在一些html中使用它们以显示在页面上。 如您所见,我已经尝试过将它们作为数组传递,但最终却是空的。 如何传递两个php变量并在我的Ajax成功调用中访问它们?

//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;
}

我的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;
}

所有这些给我空数组

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

这应该工作

$.ajax({
 url: '/reservations/admin/dateRequest',
 type: 'POST',
 data: {
    'selectedDate': selectedDate,
    'otherVariable' : 1
 }
 dataType: 'json'
}).success(function(response){
   console.log(response.times);
   console.log(response.parties);
});

由于您已经标记了laravel,因此您可以像这样在控制器中处理请求。

// Make sure you have this line in the begining of the file 
// use Illuminate\Support\Facades\Input;
public function differentDate() {


 $selectedDate = Input::get('selectedDate'); // No need to access $_POST
 $ConvertedSelectedDate =  new Carbon($selectedDate);
 $dayOfWeek = $ConvertedSelectedDate->dayOfWeek;

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



return response()->json([
   'times' => $times,
   'parties' => $parties
]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM