我試圖將一些存儲在Javascript數組中的數據傳遞給服務器進行數據庫處理。我正在使用codeignier。在Codeigniter中使用JSON將數據傳遞給服務器
JavaScript數組(unitdata_set): -
[{"unit_id":"13","unit_title":"Testsdsdf","unit_max_occupancy":"3","unit_no":"1","unit_no_adults":"1","unit_no_children":"1","unit_no_extrabed":0,"unit_mealtype":"Full Board","unit_fullboard_adult":"6000.00","unit_fullboard_child":"4000.00","unit_halfboard_adult":"1800.00","unit_halfboard_child":"1200.00","room_total":"81600.00","all_room_price":"81600.00","extra_bed_price":"4.00","meal_price":22000,"saving_amount":16320,"discounted_bedroom_price":65280},{"unit_id":"14","unit_title":"Res2","unit_max_occupancy":"12","unit_no":"0","unit_no_adults":"0","unit_no_children":"0","unit_no_extrabed":0,"unit_mealtype":"Bed and Breakfast","unit_fullboard_adult":"9000.00","unit_fullboard_child":"7000.00","unit_halfboard_adult":"7000.00","unit_halfboard_child":"5000.00","room_total":"86400.00","all_room_price":"0.00","extra_bed_price":"","meal_price":0,"saving_amount":0,"discounted_bedroom_price":0}]
Ajax的功能: -
function sendBooking(){
var boooking_url = "<?php echo site_url('tempbooking/addBooking'); ?>";
var data_booking = '';
data_booking = JSON.stringify(unitdata_set);
$.post(boooking_url,{
'booking_data[]':data_booking
},function(data){
console.log(data);
});
}
我的控制器功能,其用於接收JSON數據: -
function addBooking(){
$booking_data=$this->input->post('booking_data');
foreach ($booking_data as $booking) {
$booking_attr = json_decode($booking);
echo $booking_attr->unit_id . " " . $booking_attr->unit_title;
//echo $booking_attr['unit_id'] . " " . $booking_attr['unit_title'];
}
}
的錯誤信息,我得到: -
<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message: Trying to get property of non-object</p>
<p>Filename: controllers/tempbooking.php</p>
<p>Line Number: 156</p>
</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message: Trying to get property of non-object</p>
<p>Filename: controllers/tempbooking.php</p>
<p>Line Number: 156</p>
</div>
的print_r: -
print_r($this->input->post('booking_data'));
Array
(
[0] => [{"unit_id":"13","unit_title":"Testsdsdf","unit_max_occupancy":"3","unit_no":"1","unit_no_adults":"1","unit_no_children":"1","unit_no_extrabed":0,"unit_mealtype":"Full Board","unit_fullboard_adult":"6000.00","unit_fullboard_child":"4000.00","unit_halfboard_adult":"1800.00","unit_halfboard_child":"1200.00","room_total":"81600.00","all_room_price":"81600.00","extra_bed_price":"4.00","meal_price":22000,"saving_amount":16320,"discounted_bedroom_price":65280},{"unit_id":"14","unit_title":"Res2","unit_max_occupancy":"12","unit_no":"0","unit_no_adults":"0","unit_no_children":"0","unit_no_extrabed":0,"unit_mealtype":"Bed and Breakfast","unit_fullboard_adult":"9000.00","unit_fullboard_child":"7000.00","unit_halfboard_adult":"7000.00","unit_halfboard_child":"5000.00","room_total":"86400.00","all_room_price":"0.00","extra_bed_price":"","meal_price":0,"saving_amount":0,"discounted_bedroom_price":0}]
)
無法做到這一點,因爲我使用數組來推動數據值。 'unitdata_set.push({unit_id:test.resunit_details [i] .ReservationUnitID, unit_title:test.resunit_details [i] .ResUnitTitle, unit_max_occupancy:test.resunit_details [i]。MaxOccupancy, unit_no:0,unit_no_adults:0,unit_no_adults:0,unit_no_children:0});' – user2182143 2014-12-05 21:04:12
我編輯了我的答案。 – Guillaume 2014-12-05 22:35:47
這仍然給我錯誤。 '消息:爲foreach()提供的無效參數' – user2182143 2014-12-06 04:26:16