[英]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.