使用ajax / jquery,codeigniter将数据从视图传递到控制器

时间:2013-03-17 13:35:19

标签: jquery codeigniter

我只想用ajax将数据传递给控制器​​,ajax由视图中的下拉菜单选择。这是我的尝试http://pastebin.com/KnLqW8Pc

    //// controller ///////    
<?php         
    class Ilce extends CI_Controller{         
            public  $ilceid;         
            public function __construct()
            {
                    parent::__construct();
                    $this->load->model('ilce_model');
            }

            public function index()
            {      
                    $this->load->helper(array('form'));
                    $data['ilce'] = $this->ilce_model->ilce_getir();

                    $this->load->view('ilce', $data);
                    /*
    hello, i want to print here to data which will select in view and passed by jsfunc file ( ajax ) to here again...
    */       

            }               
    }


/////////////////////////////////view/////////////////////////////////
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <link rel="stylesheet" type="text/css" href="style/reset.css"/>
    <link rel="stylesheet" type="text/css" href="style/page.css"/>
    <link rel="shortcut icon" href="img/3burcak.ico"  />
        <script type="text/javascript" src="<?php echo base_url();?>js/jquery.js"></script>
        <script type="text/javascript" src="<?php echo base_url();?>js/jfunc.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>3Burçak Ltd.Şti.</title>

    </head>
    <?php echo  form_open('ilce/index')?>






    <select id="ilce" name="ilce">
    <option value="">Select Town</option>
    <?php foreach ($ilce as $x):?>
    <option value="<?php echo $x['id']?>"><?php echo $x['ad']?></option>
    <?php endforeach?>
    </select>
    <select id="semt">
    <option value="">Please select Town first.</option>
    </select>






////////////////////////////////////////////////////////jsfunc

(文档)$。就绪(     函数(){

    /* il ilce arama*/
    $("#ilce").change(
        function(){
           if($("#ilce").val()!="-1"){
              var ilceid=$("#ilce").val();
               $.post('ilce/index',{"ilceid":ilceid},function(output){
                    $('#semt').remove();
                    $('#semt').append(output);

               });

           }
        });

        /* il ilce arama bitti*/





    });

2 个答案:

答案 0 :(得分:0)

我的朋友,您发布的代码格式太难以理解,但据我所知,您希望将视图信息传递给ajax请求。如果是这种情况,那么这样做的方法就是将一个页面称为你的ajax请求,在这种情况下你的控制器是“Ilce”。这里假设您正在请求索引页面,因此加载视图,例如$ this-&gt; load-&gt; view('xyz',$ data);这将通过你想要的结果。

您还可以查看本教程,详细说明如何使用Code Igniter和jQuery Ajax获取ajax数据:http://amitavroy.com/justread/content/articles/getting-ajax-data-using-views-codeigniter

希望这个帮助

答案 1 :(得分:0)

你无法从AJAX调用中加载这样的视图。创建一个单独的函数将数据返回到视图,然后您的追加将起作用。

的jQuery

$("#ilce").change(
     function(){
        if($("#ilce").val()!="-1"){
            var ilceid=$("#ilce").val();
            $.post('ilce/ajax_return_data',{"ilceid":ilceid},function(output){
                    $('#semt').remove();
                    $('#semt').append(output);

               });

       }
    });

控制器

function ajax_return_data()
{
    $this->load->helper(array('form'));
    $data['ilce'] = $this->ilce_model->ilce_getir();
    print json_encode(array('output'=>$data['ilce']));
}
相关问题