Hi guys..i am just dealing with codeigniter framework..

Could you guys tell me where exactly should i put the ajax/javascript file in the codeigniter directory?

I mean could it be in the application folder or whatever, or it doesnt matter?
because i want to know which is the best practice to apply.

Thank you :)

Dani AI

Generated

Short answer: keep client-side JavaScript as public assets and handle AJAX on the server with a CodeIgniter controller method (not a standalone reply.php). This keeps your static files cacheable and served directly by the webserver, and it lets server endpoints use CI features (database, input cleaning, sessions, CSRF) consistently. asked about the reply.php approach and started the asset-organizing idea — the practical next step is to separate assets from controllers and route AJAX calls through CI.

Example server-side pattern (application/controllers/Api.php):

class Api extends CI_Controller {
    public function reply()
    {
        $this->load->model('reply_model');
        $data = $this->input->post(null, true); // sanitized input
        $ok = $this->reply_model->insert($data);
        $this->output
             ->set_content_type('application/json')
             ->set_output(json_encode(['success' => (bool) $ok]));
    }
}

Client-side example (in a public JS file under e.g. assets/js):

$.ajax({
  type: 'POST',
  url: '/index.php/api/reply',
  data: { message: 'hello' },
  dataType: 'json'
}).done(function(resp){
  if (resp.success) { /* handle success */ }
});

Quick troubleshooting and best practices:

  • Use a public assets folder (assets/js, assets/css, assets/img) and version files for cache busting.
  • Load the CI database in autoload.php or via $this->load->database() inside the controller — do not require a separate DB config from a random PHP file.
  • If CI CSRF is enabled, inject the token into AJAX requests (or send it in headers).
  • If AJAX returns unexpected text, check for PHP notices/warnings (they break JSON); set display_errors off in production and inspect server logs.
  • If you must keep a legacy standalone PHP endpoint, bootstrap CI inside it or forward the call to a controller to avoid duplicate config and security gaps.

This keeps code organized, secure, and easier to maintain as your app grows.

Recommended Answers

All 4 Replies

By ajax you mean .js file? It doesn't matter. Put near all your .js files. I have a folder in root directory called js and there I put all js files.

P.S. hehe, 100 posts by me alredy :)

By ajax you mean .js file? It doesn't matter. Put near all your .js files. I have a folder in root directory called js and there I put all js files.

P.S. hehe, 100 posts by me alredy :)

HI, thanks for answering me :)
em but i got one more question, regarding the ajax file, if i use the ajax with jquery then when i need to do for example like this :

type: "POST",
url: "<?php echo base_url();?>external/reply.php"

and my reply.php file is located in the root directory,and its require the database connection, will then i need to use the codeigniter database library whenever i want to interact with the database?because when i use the normal way without the codeigniter functions my codes wont work and i need to configure my database by making config.php file and then link it to my reply.php.

any help? thank you :)

ill then i need to use the codeigniter database library whenever i want to interact with the database?

I don't know. But why don't you put this code in some controller function and then use the CI's database functions?

ouh okay2,i'll try to do it..Thanks so much for helping :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.