2014-12-05 79 views
0

我試圖將一些存儲在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}] 
) 

回答

0

也許是因爲你的JSON對象是一個數組?

function addBooking(){ 
    $booking_data=$this->input->post('booking_data'); 

    foreach ($booking_data as $booking) { 
     //NEW: 
     $booking_attr = json_decode($booking[0]); 

     echo $booking_attr->unit_id . " " . $booking_attr->unit_title; 
    } 
} 
+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

+0

我編輯了我的答案。 – Guillaume 2014-12-05 22:35:47

+0

這仍然給我錯誤。 '消息:爲foreach()提供的無效參數' – user2182143 2014-12-06 04:26:16

0

試圖改變自己的控制器

function addBooking() 
{ 
    $booking_data=json_decode($this->input->post('booking_data')); 
    foreach ($booking_data as $booking) 
    { 
     $booking_attr = ($booking); 
     echo $booking_attr->unit_id . " " . $booking_attr->unit_title; 
     //echo $booking_attr['unit_id'] . " " . $booking_attr['unit_title']; 
    } 
} 

並改變這樣

你的JavaScript代碼:

[ {JSON} ] 

儘量使你訪問JSON數組中修改你的PHP函數

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); 

     }); 

    } 
+0

仍然無法正常工作。我收到這個錯誤。 '

甲PHP錯誤遇到

嚴重性:警告

消息:json_decode()預計參數1是字符串,數組給定

文件名:控制器/ tempbooking.php

行號:152

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: controllers/tempbooking.php

Line Number: 154

' – user2182143 2014-12-05 21:01:51

+0

在控制器功能的開始處輸出$ this-> input-> post('booking_data')。 – 2014-12-05 22:07:00

+0

''{「unit_id」:「13」,「unit_title」:「Testsdsdf」,「unit_max_occupancy」:「3」,「unit_no」:「1」,「unit_no_adults」:「1」,「unit_no_children」:「1 「unit_mealtype」:「半膳」,「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「:6600,」saving_amount「:16320,」discounted_bedroom_price「:65280},' – user2182143 2014-12-06 03:44:11

0

F或者某種原因,json_decode不能解碼包含對象的數組。如果您使用javascript數組將它傳遞給服務器端/ php,請首先將您的數組添加到對象中。請按照以下步驟完成此操作。

  1. 請將您的數組添加到對象。如下: -

    var unitdata_set = []; 
    
    var obj_userbooking_data = {"bookings":unitdata_set}; //Added the above array into a new object and passed it to the server 
    
  2. 確保JSON.stringify對象。

    data_booking = JSON.stringify(obj_userbooking_data);

所以功能變得

function sendBooking(){ 


        var boooking_url = "<?php echo site_url('booking/addBooking'); ?>"; 
        var data_booking = ''; 
        data_booking = JSON.stringify(obj_userbooking_data); 

        $.post(boooking_url,{ 

         'booking_data':data_booking 

        },function(data){ 
          console.log(data); 

        }); 



       } 
  • 使用json_decode到被轉換成JSON字符串,在客戶端側透過JSON.stringify對象進行解碼。因此,您的服務器端/ PHP /笨控制器功能變爲: -

    function addBooking(){ 
         $booking_data=$this->input->post('booking_data'); 
         $booking_data=json_decode($booking_data,true); 
         echo 'Your Data: ' . $booking_data[0]['unit_id']; 
    } 
    
  • 相關問題