Tags
PHP , AJAX
Asked 3 years ago
17 Jun 2021
Views 613
Harley

Harley posted

Basic PHP and AJAX

Basic PHP and AJAX
hanuman

hanuman
answered Apr 28 '23 00:00

You can execute a PHP file using an Ajax request by using the $.ajax() function in jQuery. Here's an example code snippet that shows how to execute a PHP file named "example.php" using an Ajax request:



$.ajax({
    url: 'example.php',
    type: 'POST',
    data: {
        param1: 'value1',
        param2: 'value2'
    },
    success: function(response) {
        console.log(response);
    },
    error: function() {
        console.log('Error occurred while making Ajax request.');
    }
});

In the above code, we use the $. ajax () function to make an Ajax request to the PHP file "example.php". The url parameter specifies the URL of the PHP file, and the type parameter specifies the HTTP method to be used (in this case, we're using the POST method). The data parameter specifies the data to be sent to the server (in this case, we're sending two parameters named "param1" and "param2" with values "value1" and "value2", respectively).

The success callback function is called when the Ajax request is completed successfully, and the response parameter contains the response from the server. We're using console.log() to print the response to the browser console. If an error occurs while making the Ajax request, the error callback function is called instead.

You can replace the console.log() statements with your own code to handle the response from the server as needed.
Post Answer