如何在php中使用json将数据传递给控制器​​进行查看?

时间:2014-03-27 16:27:08

标签: php jquery ajax json codeigniter

我使用codeigniter。我需要将数据传递给我的控制器以某种方式查看。我设法将视图传递给控制器​​(在视图中,当下拉选择值使用ajax传递给控制器​​时) 这是我的HTML代码

<div class="form-group">
   <label for="lastname" class="control-label">Your Packages</label>
      <?php if(isset($tourbuddy_packages)){?>
           <select id="itemType_id" class="form-control input-sm" name="tripbuddy_PackageTitle" onChange="disp_text()">
             <?php
                  foreach ($tourbuddy_packages as $packages) {?>
                     <option value="<?php echo $packages['PackageID'] ?>"><?php echo $packages['PackageTitle']?></option>
                   <?php } ?>
            </select>
          <input type="hidden" name="PackageID" id="country_hidden">
          <?php }  else { ?>
                 <select class="form-control input-sm" name="tripbuddy_PackageTitle">
                        <option>Add Packages</option>
                 </select>
           <?php } ?>
</div>

当下拉选择了一个值时,我使用ajax和java Script

将数据传递给控制器
$("#itemType_id").change(function() {
            $.ajax({
                url : "feature/tea/",
                method: "POST",
                data: "id=" + $(this).val(),
                success: function(response) {
                    // handle
                }
            })  
});

在控制器中选择vale传递给tea方法

 public function tea()
    {

        $this->session->set_userdata(array('tripbuddy_PackageID'=>$_POST['id']));


    $package_data = $this->ci->package_model->get_package($_POST['id']);
    $package_cat =  $this->ci->package_model->get_package_categories();
    $data = array();
    $data['tourbuddy_selected_package'] = $package_data[0];
    $data['tourbuddy_selected_package_cat'] = $package_cat;
    //echo $data['package']['AlbumID'];
    $data['tourbuddy_selected_photos'] = $this->photo->get_package_photo_stream($data['tourbuddy_selected_package']['AlbumID']);    
    //echo var_dump($data['photos']);

    echo json_encode($data);

    }

现在我需要将$ data数组传递给我的视图而不刷新视图页面我该怎么做?需要快速帮助

2 个答案:

答案 0 :(得分:1)

首先,您需要将正确的标头添加到tea()函数中,因为它将返回json

public function tea()
{
    header('Content-Type: application/json');

   //...
}

然后,您需要将dataType参数添加到您的ajax调用

$("#itemType_id").change(function() {
    $.ajax({
        url : "feature/tea/",
        method: "POST",
        dataType: 'json', //Added this
        data: "id=" + $(this).val(),
        success: function(response) {
            // handle
        }
    })  
});

在您的成功功能中,您将能够访问

等数据
success: function(response) {
    response.tourbuddy_selected_photos.data
}

答案 1 :(得分:1)

控制器

class Feature extends CI_Controller
    {
        public function tea()
        {
            $post = $this->input->post(); //do some form validation here

            $model = Model::get($post); // do all business logic in the model

            if(!$model){
                //return a Response header rather than a 404View
                return $this->output->set_status_header(404); 
            }

            $responce = array(
                'something'  =>  $model->something
            );

            return $this->output
                ->set_content_type('application/json')
                ->set_output(json_encode($responce))
                ->set_status_header(200);
        }
    }

未经测试的Javascript

 var URL = <?php echo site_url(); ?>// Global URL variable

    (function($){

        var Form = {
            init : function(){
                this.Form = $("form#formId"),
                this.ItemType = this.Form.find("#itemtype_id");

                this.ItemType.on('change', $.proxy(this.change, this));
            },
            /**
             * -----------------------------------------------------------------
             * Bind the ItemTypeId change event to this function using $.proxy
             * Ajax return's a deffered(promise) so capture it, and do stuff
             * -----------------------------------------------------------------
            **/
            change : function(event){
                this.doAjax(event.target).then(
                    function( data ) // success
                    {
                        $(this).html('Success'); // $(this) -> context
                    },
                    function( reason ) //fail
                    {
                        switch(reason.code)
                        {
                            case 404:
                            default:
                                $(this).html('no results found');
                            break;

                        }
                    }
                );
            },
            /**
             * -----------------------------------------------------------------
             * Make the ajax request a wait for it to return a promise
             * -----------------------------------------------------------------
            **/
            doAjax : function( target ){
                var data = {
                    id : target.id
                }
                return $.ajax({
                    cache: false,
                    url : URL + 'feature/tea/',
                    context : target,
                    method: "POST",
                    data : data,
                    dataType : 'json',
                }).promise();
            }
        }

        Form.init();

    }(jQuery));