javascript - Passing data to server using JSON in Codeigniter -
i'm trying pass data stored in javascript array server database processings. i'm using 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 , 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:-
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); }); }
my controller function used receive json data:-
function addbooking(){ $booking_data=$this->input->post('booking_data'); foreach ($booking_data $booking) { $booking_attr = json_decode($booking); echo $booking_attr->unit_id . " " . $booking_attr->unit_title; //echo $booking_attr['unit_id'] . " " . $booking_attr['unit_title']; } }
the error message get:-
<h4>a php error encountered</h4> <p>severity: notice</p> <p>message: trying 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 encountered</h4> <p>severity: notice</p> <p>message: trying 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 , 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}] )
try change controller
function addbooking() { $booking_data=json_decode($this->input->post('booking_data')); foreach ($booking_data $booking) { $booking_attr = ($booking); echo $booking_attr->unit_id . " " . $booking_attr->unit_title; //echo $booking_attr['unit_id'] . " " . $booking_attr['unit_title']; } }
and change javascript code this
function sendbooking() { var boooking_url = "<?php echo site_url('tempbooking/addbooking'); ?>"; var data_booking = ''; data_booking = (unitdata_set); $.post(boooking_url,{ 'booking_data':data_booking },function(data){ console.log(data); }); }
Comments
Post a Comment