June 20, 2025
Ajax post request with jQuery.post method
Using jQuery's post() method, we can send data to the server through an AJAX request. It is commonly used to insert new data into a database. The data is sent in the HTTP request body, not in the URL as a query string, which makes it more secure for sensitive information. It takes 3 arguments, first the URL from where we need to send data to server. Second data which we need to pass to server, third is callback function when the request gets complete then this callback function gets called. Example of jQuery post method is mentioned below:
jQuery.post() method example
<html>
<head>
<title>Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<input type="text" name="stud_name" id="stud_name" placeholder="Enter student name">
<input type="text" name="stud_class" id="stud_class" placeholder="Enter student class">
<button id="btnget">Save Student Data</button>
<script>
$(document).ready(function () {
$('#btnget').click(function() {
$.post("saveStudentDetails.php", { stud_name: $('#stud_name').val(), stud_class: $('#stud_class').val()}, function(response) {
alert("Server response:", response);
});
});
});
</script>
</body>
</html>