Я пытаюсь передать некоторые данные, которые хранятся в массиве Javascript на сервере для обработки базы данных. Я использую codeignier.
Javascript Array (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}] )