0

I'm new in web development. How can i load my php file that contains html in order to change the span content. Im using codeigniter by the way.

I always get an error.

here is my js file

$(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.

1 Answer 1

1

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));
}
Sign up to request clarification or add additional context in comments.

3 Comments

still, i got an error. i don't know what it is but when i check the error it says undefined
what is undefined? data?
sorry, i don't know but i just get the responsetext in the function ajaxError and all i got i undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.