When I first learned AJAX, I was also teaching myself the languages of PHP, JavaScript, SQL, HTML, and CSS. I followed my coworkers existing web apps as examples of how to write my own web applications. Now that I have a better grasp on the languages and how they work, I have begun to question the existing script structure that my coworkers and I use. Basically, we have all of our calls to one PHP file, and send an event in the url.
It is structured something like this:
Url:wepappname_ajax.php?event=dosomething
PHP:
If ($event == dosomething) {
//do something
} elseif ($event == dosomethingelse) {
//do something else
}
Is a giant if statement really the route we should be taking? Should we section everything off into functions instead, or should we be making seperate PHP files for every different event?
I think in any language/framework/application it is good to try to have single responsibility of components, for your case it would mean to have each of the ajax “events” in separate files.
you could have files like:
/ajax/posts.php
/ajax/userInfo.php
...
each file could then require some core/common functionality from one location
it makes it much more readable/easier to maintain this way
you could also try using a framework that enforces this kind of structure.
hope this helps 🙂
3