在Codeigniter中使用JSON将数据传递到服务器 [英] Passing data to server using JSON in Codeigniter

查看:1298
本文介绍了在Codeigniter中使用JSON将数据传递到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将存储在Javascript数组中的一些数据传递给服务器进行数据库处理。我使用codeignier。

I'm trying to pass some data which is stored in Javascript array to the server for database processings. I'm using codeignier.

JavaScript数组(unitdata_set): -

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}] 


$ b b

Ajax函数: -

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

          });

      }

我的Controller函数用于接收json数据: -

My Controller function which is used to receive the json data:-

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'];
                }


        }

我得到的错误信息: -

The error message that i get:-

<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 :-

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}]
)


推荐答案

json_decode不解码包含对象的数组。如果你使用javascript数组将其传递到服务器端/ php,请首先将你的数组添加到对象。按照以下步骤完成此操作。

For some reason json_decode does not decode arrays which contains objects. If you are using javascript array to pass it to server-side / php please add your array to an object first. Follow below steps to get this done.


  1. 请将数组添加到对象。如下: -

  1. Please add your array to an object. As follows:-

var unitdata_set = [];

var obj_userbooking_data = {"bookings":unitdata_set}; //Added the above array into a new object and passed it to the server


  • 到JSON.stringify对象。

  • Make sure to JSON.stringify the object.

    data_booking = JSON.stringify(obj_userbooking_data);

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




    1. 使用json_decode解析被转换为JSON字符串的对象,在客户端使用JSON.stringify。因此,您的服务器端/ php / Codeigniter控制器函数变为: -

    1. use json_decode to decode the object which is converted into JSON string, using JSON.stringify in the client side. Thus, your server-side / php / Codeigniter Controller function becomes:-

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


  • 这篇关于在Codeigniter中使用JSON将数据传递到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆