I was wondering, is it possible to run a PHP file’s script when an AJAX event is called?
I was thinking, if on the AJAX error
that it would send the data to the error.php
file and log the error, send an email to the admin, and whatever other notifications I wanted.
I can obviously can do this via javascript, but was wondering if while sending an AJAX call to a PHP file already, is it possible to piggyback and so it for each function?
$.ajax({
method: "POST",
data: mb_ajax_form_data,
contentType: false,
processData: false,
error: function(error){
<?php include error.php; ?>
}
});
error.php
// send email
mail();
// add to log file
fopen();
// etc.
2
Answers
You can do something like this
first function to post form values
Then do second function here, when first function success it will start second one.
The easiest way to do this is run a XHR request
create this file in PHP for your error script
This is the XHR Ajax send with a special method to extract the form values
and example of a html form to invoke the XHR
This is the same method as we use in a rest API and should help your purposes with error and script handling