I'm making a lot of $.ajax calls, and I'm trying to create a function, so I don't have to keep typing out the full $.ajax format.
I'd like to create a function similar to this:
function php(task, data=false) {
if(!data) { data = ""; } else { data = "&"+data; }
$.ajax({ url: 'actions.php',
data: '&task='+task+data,
type: 'post',
success: function(output) { return output; },
error: function(xhr, e){ alert(e); }
});
}
The goal is to return the output, so I can use it somewhere else. For Example:
function doSomething() {
var test = php("test");
alert(test);
}
On the actions.php page, there is a task called "test", that's sole purpose is to echo out: "TESTING 123".
If I use $.ajax in "doSomething()" (changing the success potion to simply alert(output) - then I would see an alert box with the words "TESTING 123" (working as intended)
However, when I attempt to create the "php(task, data)" function, and return the output to a variable, my alert box says "UNDEFINED".