简体   繁体   English

在Codeigniter中使用Ajax加载php文件

[英]load php file using ajax in codeigniter

I'm new in web development. 我是Web开发的新手。 How can i load my php file that contains html in order to change the span content. 我如何加载包含html的php文件以更改span内容。 Im using codeigniter by the way. 我顺便用了codeigniter。

I always get an error. 我总是会出错。

here is my js file 这是我的js文件

$(document).ready(init);

function init(){
$('#home').click(doAjax);
}


function doAjax(){  
        $.ajax({
            data:{
                'n':1
            },
            type: 'get',
            success: ajaxSuccess,
            error: ajaxError,
            url: 'http://localhost/index.php/main/ajax'
        });


        function ajaxSuccess(data, textStatus){
            alert("sucess");
            $('#main-span').load(data);         
        }

        function ajaxError(data, textStatus){
            alert("error");
            $('#main-span').html('error');
        }





    }

controller 调节器

public function ajax(){
        $input = $this->input->get('n');
        $input = '/application/views/student_main.php';
            echo $input;
}

maincontent - this is where i want the span to update 主要内容-这是我要更新跨度的地方

<div class="container-fluid">
<div class="row">

<div class="col-sm-3 col-md-2 sidebar" id ="nav-sidebar">
<?php include('/application/views/navigation.php');?>
</div>

<span id="main-span">
        <?php include('/application/views/evaluation.php');?>
</span>


</div>
</div>

thanks in advance for those who can answer my problem. 在此先感谢那些可以回答我的问题的人。

Try this code: 试试这个代码:

$.ajax({
        url: "http://localhost/index.php/main/ajax",
        type: "get",
        data: {'n':1},

        success: function(data,txtstatus){
            alert("success");
            $('#main-span').load(data.next_url);   
        },
        error:function(){
            alert("failure");
            $('#main-span').html('error');
        }
    });

In your controller: 在您的控制器中:

public function ajax(){
        $input = $this->input->get('n');
        $input = '/application/views/student_main.php'; // you can't access view directly. call this view from a controller.
        echo json_encode(array("next_url"=>$input));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM