【问题标题】:codeigniter view not loading using jquery $postcodeigniter 视图未使用 jquery $post 加载
【发布时间】:2015-02-26 12:02:56
【问题描述】:

我需要向控制器发布一些值并使用 jquery $post 方法加载视图,但是页面没有加载,但我可以在浏览器的控制台中看到该页面。

我的代码:

$('#fm_reservation').submit(function(){

    var days = getTimeDiff();
    $.post(base_url+'vehicle/getVehicles',{'vehicle_name':vehicle_name,'days':days},function(data){
    });
  return false;
});

控制器:

public function getVehicles(){

 $vehicle_name = $this->input->post('vehicle_name');
 $days = $this->input->post('days');

 $where = array('vehicle_name'=>$vehicle_name);
 $vehicle_arr = $this->almana_model->getVehicles($where);
 $data['vehicle_arr'] = $vehicle_arr;
 $this->load->view('vehiclec/vehicle.html');

}

请帮忙

【问题讨论】:

    标签: jquery codeigniter http-post


    【解决方案1】:

    使用codeigniter,您可以将视图加载到这样的变量中:

    $html = $this->load->view('vehiclec/vehicle.html',array(),true);
    die($html);
    

    这里只是说明一下,vehicle.html 是错误的,codeigniter 从 php 文件加载视图,因此视图文件应该是 vehicle.php 并且调用“vehicle/vechicle”

    view 方法的最后一个参数中的布尔值“true”,告诉 codeigniter 缓冲视图文件而不是显示它。然后,您可以将 html 作为响应发送回您的 ajax 请求,并使用您的 jquery 更新 html:

    $('#fm_reservation').submit(function(){
    
        var days = getTimeDiff();
        $.post(base_url+'vehicle/getVehicles',{'vehicle_name':vehicle_name,'days':days},function(data){
        $("#html-element").html(data);
        });
      return false;
    });
    

    【讨论】:

      【解决方案2】:

      它不会加载你的视图,如果你想加载你可以通过 jquery 或重定向使用表单发布,它将在控制台中输出你的视图。

      示例:使用 ajax 请求发送表单数据

      $.post( "test.php", $( "#testform" ).serialize() );
      

      http://api.jquery.com/jquery.post/

      【讨论】:

        猜你喜欢
        • 2013-08-30
        • 2013-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多