dtjmsy
dtjmsy

Reputation: 2752

codeigniter Click button to call a view

I am having a view with 2 buttons in my codeigniter view:

    <div class="btn-main col-md-3 col-md-offset-3"> 
        <button id="simu-mono" type="button" class="btn btn-default">SIMULATION MONO SITE</button>
    </div>
    <div class="btn-main col-md-3"> 
        <button id="simu-multi" type="button" class="btn btn-default">SIMULATION MULTI SITE</button>
    </div>

I would like to call another a controller to launch then a view when the button is clicked

I tried out to call the controller simu_mono by javascript, putted on /controller/simu_mono.php but doesn' t work

$(document).ready(function(){
    $("#simu-mono").click(function(){
        type:'GET',
        url:'simu_mono'
    });

    $("#simu-multi").click(function(){

    });
 });

simu_mono.php:

<?php
class simu_mono extends CI_Controller {

    public function index()
    {
            $this->load->view('simu_mono');
            echo 'Hello World!';
    }
}
?>

Thanks for your helps

Cheers

Upvotes: 0

Views: 3594

Answers (2)

Kyslik
Kyslik

Reputation: 8385

Please, if u want to redirect only use following code:

$(document).ready(function(){
    $("#simu-mono").click(function(){
         window.location = base_url + "/simu_mono";
    });

    $("#simu-multi").click(function(){
         window.location = base_url + "/simu_multi";
    });
 });

Note that you might need base_url, use this snippet to load base_url in JavaScript variable

<script>
    base_url = <?= base_url()?>
</script>

put code above in some kind of view that is loaded always (before any other JavaScript code is executed)

Additional step would be to set up routes that take care of ugly underscore symbol (_)

something like:

routes.php

$route['simu-mono'] = "simu_mono";
$route['simu-multi'] = "simu_multi";

this way you go to your page and controller following way: yourserver.ufo/simu-mono and yourserver.ufo/simu-multi

Upvotes: 1

Federico J.
Federico J.

Reputation: 15902

You're not doing any class of AJAX call within your javascript. I assume you're using jQuery, so, your call should be something like:

$("#simu-mono").click(function(){
    $.ajax({
        url: "http://your-url.com/controller/method",
        type: 'post',     // <- Or get option, whatever you prefer
        dataType: 'json', // <- This is important to manage the answer in the success function
        //data: { param1: "value1", param2: "value2"}, <- You could add here any POST params you wanted
        success: function(data){            
            if (data.view) {
                $('#here_view').html(data.view); // <- '#here_view' would be the id of the container
            }
            if (data.error){
                console.log(data.error);
            }
        }
    });
});

This will call your method, where you will have to indicate you want to pass the view:

<?php
class simu_mono extends CI_Controller {

    public function index()
    {
        $return = array(
            'view' => $this->load->view('simu_mono')
        );

        echo json_encode( $return );
    }
}
?>

json_encode will allow you easily pass vars and data from PHP to your javascript, and manage them in the client view. As you see in the javascript, I added data.error, this is just in case you'll have more logic, maybe change the view you're sending, send an error if you sent data and want to control them, etc.

Of course, in your javascript you could take the url from the clicked button, and in data.view parat of the success function, you may print in the screen a modal, send the view to a container, whatever you wanted, XD

Upvotes: 0

Related Questions