Approach: When the button gets clicked, we have initialized the XMLHttpRequest object, which is responsible for making AJAX calls. Then, we have to check if the readyState value is 4, which means the request is completed, and we have got a response from the server.
Next, we have checked if the status code equals 200, which means the request was successful. Finally, we fetch the response which is stored in the responseText property of the XMLHttpRequest object. After setting up the listener, we initiate the request by calling the open method of the XMLHttpRequest object. Then call the send method of the XMLHttpRequest object, which actually sends the request to the server. When the server responds, you can see the text displaying the response from the PHP file.
PHP code: The following is the code for “data.php” file used in the above HTML code.