I'm using Javascript to make XMLHttpRequests to various PHP files across my app, such as add.php, update.php, and delete.php. It feels redundant to have (3) different files being called. Is there a way I can put all these 3 files, add/update/delete, inside a single PHP file, like "main_operations.php", and then just make XMLHttpRequests to that single file each time, and have some sort of logic that only executes the part of the file I need? Example, maybe I can set a session variable in the xmlhttpprequest, like $_SESSION['ad_operation'] and then in the "main_operations.php" file use that variable to only run the ad operation?
You can do it as Chris P said, it is as simple as that.
Example:-
$.post("url",{ action: "update/delete/add", id : "some_ID"},
function(data,success){//Do something});
PHP file
<?php
if(isset($_POST['action'])){
if($_POST['action'] == "add"){
//Do add operation
}
else if($_POST['action'] == "update"){
//Do update operation
}
else if($_POST['action'] == "delete"){
//Do delete operation
}
}
?>
This helps you to handle the request and control the response.