How to send button value to PHP backend via POST using ajax ? Last Updated : 31 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The purpose of this article is to send the value of the button to PHP back-end using AJAX in an HTML document.Approach: Create a button in HTML document and assign an Id to it. In JavaScript file add an event listener to button i.e click. Then the request is made to PHP file using jQuery Ajax.HTML code: HTML <!-- HTML Code --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <!-- JavaScript file --> <script src="script.js"></script> <!-- jQuery Ajax CDN --> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> </head> <body> <!-- Button --> <button id="btn" value="hello world"> Click On me! </button> </body> </html> JavaScript code: The following is the code for "script.js" file. JavaScript // Button DOM let btn = document.getElementById("btn"); // Adding event listener to button btn.addEventListener("click", () => { // Fetching Button value let btnValue = btn.value; // jQuery Ajax Post Request $.post('action.php', { btnValue: btnValue }, (response) => { // response from PHP back-end console.log(response); }); }); PHP code: The following is the code for "action.php" file. PHP <?php // Checking, if post value is // set by user or not if(isset($_POST['btnValue'])) { // Getting the value of button // in $btnValue variable $btnValue = $_POST['btnValue']; // Sending Response echo "Success"; } ?> Output: Comment More infoAdvertise with us Next Article How to Send an Image using Ajax ? A asmitsirohi Follow Improve Article Tags : PHP Technical Scripter 2020 HTML5 jQuery-Questions PHP-Questions +1 More Similar Reads How to add more values to array on button click using PHP ? In this article, we will learn to add elements to an existing array with the click of a button using PHP. PHP is a server-side language, and it only responds to requests (GET, POST, PUT, PATCH, and DELETE). The button click action happens as a part of the client-side to directly call a PHP function. 3 min read How to submit a form without using submit button using PHP ? In this article, we will see how to submit the form without clicking the submit button, along with understanding the different ways to accomplish this task through implementation.The form can be submitted without using submit button by implementing a specific event attribute or by clicking the link. 3 min read How to Send an Image using Ajax ? Ajax stands for Asynchronous Javascript and XML and is used to make indirect requests to other origins. It can help you to perform a change to the web page dynamically.We can make an AJAX request with a special object called XMLHttpRequest which provides us with different methods to create an HTTP r 5 min read How to get information sent via post method in PHP ? In this article, we will learn to get information via the post method in PHP. In PHP, we can use the $_POST method as a superglobal variable that is operated to manage form data. After we click on submit button and the page will send the data through the post method. We can use the data after storin 2 min read How to Replicate Postman POST API Request using AJAX ? In this project we'll mimic the action of submitting data using POST to a server from a web page. We replicate this process using AJAX (Asynchronous JavaScript and XML) directly in a web page, we're embedding similar functionality within our own interface. This involves setting up HTML elements like 4 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 Like