0

我只想使用 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

$(文档).ready(函数(){

    /* 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*/





    });
4

2 回答 2

0

You're not going to be able to load a view like that from an AJAX call. Make a separate function to return the data to the view then your append will work.

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

               });

       }
    });

Controller

function ajax_return_data()
{
    $this->load->helper(array('form'));
    $data['ilce'] = $this->ilce_model->ilce_getir();
    print json_encode(array('output'=>$data['ilce']));
}
于 2013-03-17T14:41:04.803 回答
0

我的朋友,您发布的代码格式太差,无法理解,但据我了解,您希望将查看信息传递给 ajax 请求。如果是这种情况,那么执行此操作的方法将页面调用为您的 ajax 请求,在本例中为“Ilce”,该请求位于控制器内部。这里说你正在请求索引页面,所以加载一个视图,例如 $this->load->view('xyz', $data); 这将使您获得所需的结果。

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

希望这有帮助

于 2013-03-17T14:01:36.820 回答