How to get return text from PHP file with ajax ? Last Updated : 06 Dec, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will see how to get return the text from the PHP file with ajax. Ajax is an acronym for Asynchronous JavaScript and XML is a series of web development techniques that build asynchronous web applications using many web technologies on the client-side. Without reloading the web page, AJAX enables you to send and receive information asynchronously. 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. Example: This example describes returning the text from PHP with AJAX. HTML <!DOCTYPE html> <html> <body> <button type="button" id="fetchBtn">Click Me!</button> <p id="txt"></p> <script> let fetchBtn = document.getElementById('fetchBtn'); fetchBtn.addEventListener('click', buttonClickHandler); function buttonClickHandler() { // Instantiate an xhr object var xhr = new XMLHttpRequest(); // What to do when response is ready xhr.onreadystatechange = () => { if(xhr.readyState === 4) { if(xhr.status === 200) { document.getElementById("txt").innerHTML = xhr.responseText; } else { console.log('Error Code: ' + xhr.status); console.log('Error Message: ' + xhr.statusText); } } } xhr.open('GET', 'data.php'); // Send the request xhr.send(); } </script> </body> </html> PHP code: The following is the code for the "data.php" file used in the above HTML code. data.php <html> <body> <?php echo '<h1>Welcome to GfG</h1>'; ?> </body> </html> Output: Comment More infoAdvertise with us Next Article How to get return text from PHP file with ajax ? P priyavermaa1198 Follow Improve Article Tags : PHP Technical Scripter 2020 PHP-Questions Similar Reads How to Return JSON from a PHP Script ? Returning JSON Data consists of converting PHP data structures like arrays or objects into a JSON format. In this article, we will explore two different approaches to Return JSON from a PHP Script. Below are the approaches to Return JSON from a PHP Script: Table of Content Using json_encode() Using 2 min read How to use an HTTP GET or POST for Ajax Calls? Sending an HTTP request to the server using AJAX is the most common way of fetching data these days. It provides us with methods to send and receive data. In this article, we are going to discuss GET and POST methods.GET method: This method is used to GET or RECEIVE the data from a file, API, etc.Ho 3 min read How to download files from an external server with code in PHP ? PHP provides many inbuilt variables or functions to perform this kind of operation. One of them is file_get_contents to download the files from the external server using PHP.file_get_contents() Function Parameters:$path: It declares the path of the file which we are going to fetch.$include_path: Thi 2 min read How to get server response from an AJAX request using jQuery ? In this article, we will see how we can use jQuery to get the server response to an AJAX request. The jQuery ajax() method implements the basic Ajax functionality in jQuery. It communicates with the server via asynchronous HTTP requests. Syntax:$.ajax(url);$.ajax(url,[options]);Parameters:url: A URL 4 min read How to write Into a File in PHP ? In this article, we are going to discuss how to write into a text file using the PHP built-in fwrite() function. The fwrite() function is used to write into the given file. It stops at the end of the file or when it reaches the specified length passed as a parameter, whichever comes first. The file 2 min read How to send a POST Request with PHP ? In web development, sending POST requests is a common practice for interacting with servers and exchanging data. PHP, a versatile server-side scripting language, provides various approaches to accomplish this task. This article will explore different methods to send POST requests using PHP. Table of 3 min read How to Send FormData Objects with Ajax-requests in jQuery ? In this article, we will see how can we send formData objects with Ajax requests by using jQuery. To send formData, we use two methods, namely, the FormData() method and the second method is serialize() method. The implementation of Ajax to send form data enables the submission of data to the server 4 min read How to set timeout for ajax by using jQuery? In web programming, the Ajax is used so that the resultant data is shown in the one part of the web page, without reloading the page. The user needs to perform the Ajax request and wants the result within a timeframe. In this scenario, the jquery timeout feature is used in the code. Session timeout 4 min read How to get form data using POST method in PHP ? PHP provides a way to read raw POST data of an HTML Form using php:// which is used for accessing PHPâs input and output streams. In this article, we will use the mentioned way in three different ways. We will use php://input, which is a read-only PHP stream. We will create a basic HTML form page wh 2 min read How to make ajax call from JavaScript ? Making an Ajax call from JavaScript means sending an asynchronous request to a server to fetch or send data without reloading the web page. This allows dynamic content updates, enhancing user experience by making the web application more interactive and responsive.There are multiple ways to make Aja 4 min read Like