I have no problem creating an AJAX call, but each url must at the moment be unique. I wish to contain multiple PHP functions within the same file, and specifically access one of the PHP functions with a particular AJAX call.
Using jQuery, at the moment I am passing the .data parameter with an integer specification, then using the $_GET method in the PHP file and running the proper function specified by the integer.
How can I do this better? I want to call a specific function amongst many in a PHP file; I do not want to have individual files for each function
I could tell you to go with the quick and dirty solution, i.e. passing a
functionparam in the call, then:But this is totally unmaintainable, because you will drown into a mess of includes, or even worse into a huge column of code you won’t be able to reuse anywhere.
I advice you to get into routing, i.e. setting up a relationship between the URLs being pointed at, and functions being called, making use of parameters if needed [like in the second example below]:
This way you are decoupling the routing itself from the response generation: you can find a better overall explanation here, then you can find a very good an thin library here you can use in your projects without relying on a whole framework.
Ah, before getting in buzzword oceans with thing like OOP, MVC, RESTful service – which all deserve to be learned of course – stick with this first concept: it was the one I really missed when I began learning all this stuff, and it was the eureka for me.