Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Is it bad to put all my Ajax code in a single PHP file?

Am working on a huge project that does many Ajax call, I have just only one file name ajax.php, in this file is all my PHP codes for Ajax. I also have a single javascript file, named function.js, this also is the only Javascript file I have, all my JS scripts are there and I have just only one form function that processes all the forms in the whole application via Ajax, any form I give the class "formprocessor" would be processed through the form function in my javascript.

I feel having a single file and multiple users accessing it concurrently would lead to some speed issues when the users are many.

I am thinking of creating many PHP files for my Ajax, i.e. each page would have it's own PHP file for Ajax call. Do I really need to do this?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I have just only one file name ajax.php, in this file is all my PHP codes for Ajax. I also have a single javascript file, named function.js, this also is the only Javascript file I have.

It is definitely not good in terms of code maintainability and readability.
Every time you need to make any changes in your system, you modify this file. Also, it is you who knows where everything is located in this file, but if you give this code to another developer, then he will have a headache.

At least, you can separate every AJAX request handler to a separate file:

/handlers/PingHandler.php
/handlers/GetUserHandler.php
index.php

In index.php you can include all handlers and pass a request to a given handler. It will make code readable, and if you need to change GetUser functionality or add new, you only need to modify / create one file.

Or you can include all common things like database connections, checks, validations, authentication etc. to separate file, and then create pages which handle the request:

/include/database.php
/include/auth.php
ping.php
get-user.php

ping.php and get-user.php can include necessary php files and work with database / auth etc.

I feel having a single file and multiple users accessing it concurrently would lead to some speed issues when the users are many.

It absolutely doesn't matter if users access different files or one.
Web server handles this for you, there is nothing about performance you should worry about.

I am thinking of creating many PHP files for my Ajax, i.e. each page would have it's own PHP file for Ajax call.

It is a good idea. If you really feel that project structure is not convenient, not extensible, not supportable, and brings you problems, then refactor it if possible.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!