If I create an "ajaxified" user interface with many ajax calls to create, rename, delete various things on my page - what is the best way to handle all these "codes behind" ajax pages? At first, I had a different .php file for each call (i.e. Candy_Rename.php, Candy_Delete.php). But I found that my code base is becoming overwhelming with the number of files created.
Now I send the “command” parameter with my request, and then I manage all the things related to Candy in a single file “ajax_Candy.php”. So, for example, my jQuery query would look like this:
$.get('ajax_Candy.asp', { command: 'insert', candyName: 'Jolly Ranchers' }, function (response) {
alert(response.candyId);
}
Is this a good way to do this?
Edit: All answers were excellent and said almost the same thing. I had to choose one, and I thought that the mention of safety is important for everyone who travels along this path - so you go. I use the switch statement as one of the mentioned users.
source
share