yohan dasa
yohan dasa

Reputation: 1

How to call codeiginiter controller from a javascript

I'm trying to change my routing page in codeiginiter. how can i change my default controller

Someone already do this?

Thanks.

Upvotes: 0

Views: 70

Answers (4)

careless.engineer
careless.engineer

Reputation: 11

By calling Ajax Api in your JavaScript file, you can call controller

for these you can follow below steps -

  1. first include jQuery in your CodeIgniter project.
  2. Now, Add Ajax call in javascript file

$.ajax({
                url: "users/get_client_details",
                type: "POST",
                data: {"userId": id},
                async: false,
                dataType: 'json',
                success: function (response) {
                    if (response.status === "false") {
                        alert("Please Enter Correct id.");
                    }
                }
            });

  1. Add routes path in routes.php

$route['users/get_client_details'] = 'users/fetch_client_details';

Now you can access your controller from JavaScript.

Upvotes: 1

munsifali
munsifali

Reputation: 1731

In the past, I have set up a route for the ajax request. Something like this:

$route['admin/search/(:any)'] = 'admin_controller/admin/do_search/$1';

Then my ajax request would look like this: AJAX Request

$.ajax({
type : 'POST',
data : 'your data',
url : '<?php array('controller'=>'abc','action'=>'you function')?>',
success :   function(data){
           alert(success)
}
});

Upvotes: 0

Erman Belegu
Erman Belegu

Reputation: 4079

If I understand you well, you wanted to change your default controller.

You can do it in application/config/routes.php

$route['default_controller'] = 'welcome'; // Change welcome with your default controller class,

Upvotes: 4

Joffrey Maheo
Joffrey Maheo

Reputation: 2919

Go to the application/config folder and open routes.php files and change this line :

$route['default_controller'] = "name_of_your_controller";

Upvotes: 1

Related Questions