将Jquery数组值发送到codeigniter控制器不工作....

时间:2014-02-15 14:55:22

标签: php jquery codeigniter

我想确定按哪个按钮。并在数据库中存储按钮id的子字符串(服务类型)。使用jquery我得到了id为button的子串。在下一步中,我想将值发送到控制器。使用以下代码我尝试这样做。但是在控制器内部没有任何价值......

Jquery的:

$('.select_service').click(
            function(){
             var service_type = this.id.substr(0,7);
                    var datas='service_type='+ service_type;
                    alert(datas);//this work fine
                    $.ajax({
                        type: "POST",    
                        url: base_url + 'select_service',
                        data: datas,
                        success: function(msg)
                        {
                        //window.location.href=base_url + 'edit_profile';

                        }
                    }); 
                return false;
            }

        );

select_service.php查看文件

...............
          <div class="row">
            <div class="col-sm-6 col-sm-offset-4">
                    <p class="submit"><input type="submit" class="select_service"  name="register" value="599 /-pm" id="list_pm_bt"></p>
            </div>
           </div>
           <div class="row">
            <div class="col-sm-6 col-sm-offset-2">
                <p>Half Yearly:</p>
            </div>
            <div class="col-sm-6 col-sm-offset-4">
                <p class="submit"><input type="submit" class="select_service"  name="register" value=" 3299 /- " id="list_hy_bt"></p>
            </div>
          </div>
          <div class="row">
            <div class="col-sm-6 col-sm-offset-2">
                <p>Annual:</p>
            </div>
            <div class="col-sm-6 col-sm-offset-4">
              <p class="submit"><input type="submit" class="select_service"  name="register" value=" 5999 /- " id="list_an_bt"></p>
            </div>
              </div>
..............

select_service.php控制器

public function index()
    {

        $this->load->view('select_service');
        $select_service =$this->input->post('data', TRUE);
            echo "select controller";//it work
            echo $select_service;//nothing to display
        if($this->input->post('data')) 
                {
                            echo "update data";//it does not work
                $this->service($select_service);
                }
    }

    public function service($select){ 
            $select_service =$select;
            $array = array('service_type'=>$select_service);
            $result = $this->register_model->add_service($array);
            redirect(base_url().'edit_profile/');
    }.......

register_model.php

function add_service($array){
        $email=$this->session->flashdata('email');
        $this->db->select("email");
        $this->db->from("store");
        $this->db->where('email', $email);
        $query=$this->db->get();
        if ($query->num_rows() > 0){
            echo $query->num_rows();
        }else{
            $store_id = $this->db->update('store',$array);
            return 1;}

3 个答案:

答案 0 :(得分:1)

使用:

$('.select_service').click(
            function(){
             var service_type = this.id.substr(0,7);
                    var datas= {tadada: 'service_type='+ service_type};
                    alert(datas);//this work fine
                    $.ajax({
                        type: "POST",    
                        url: base_url + 'select_service',
                        data: datas,
                        success: function(msg)
                        {
                        //window.location.href=base_url + 'edit_profile';

                        }
                    }); 
                return false;
            }

        );

在php中,你将tadada作为POST变量

答案 1 :(得分:1)

您的帖子密钥为service_type而不是data

$select_service =$this->input->post('service_type', TRUE);

答案 2 :(得分:0)

尝试:

$('.select_service').click(
        function(){
         var service_type = this.id.substr(0,7);
                var datas='service_type='+ service_type;
                alert(datas);//this work fine
                $.ajax({
                    type: "POST",    
                    url: base_url + 'select_service',
                    data: { data: datas },
                    success: function(msg)
                    {
                    //window.location.href=base_url + 'edit_profile';

                    }
                }); 
            return false;
        }

    );

使用$this->input->post('data')

获取控制器中数据的值