Code structure using PHP and jQuery
Hi everyone,
First off all let me say that I'm new jQuery and relatively new in PHP.
I'm developing a website with jQuery and PHP and I have a question about code structure.
I've organized the PHP code in different functions like:
file_api.php
user_api.php
content_api.php
etc...
Each file as a set of functions:
(example for file_api.php)
create_folder();
get_folder_content();
move_file();
etc...
These functions are usually called by jQuery with the $.ajax function with POST method.
Since I have several functions in PHP files I always send a parameter with the required option to know what function to call.
Example:
jQuery
-
$.ajax({
type: "POST",
url: "api/file_api.php",
data: "option=get_folder_content&folder=example,
success:
function(msg){
if(msg == 0){ alert("ok")};
}else{
alert("error");
} ,
error:
function(msg, data){
alert("failure "+msg+"\n"+data);
}
});
PHP
-
if ($option ==" get_folder_content")
get_folder_content();
My question is: Is this approach correct? Can I do this with better performace/security?
Other question is: What's the best way to test if anything goes wrong in the PHP response? There is some kind of "throw exception" from PHP and catch it in jQuery?
Thanks in advice.
Sorry if my English isn't perfect
