Ajax in Jquery
Awesome! Let’s dive into AJAX in jQuery — one of its most powerful features for building dynamic websites without page reloads.
? What is AJAX?
AJAX (Asynchronous JavaScript And XML) allows web pages to:
Send or receive data from a server
Without refreshing the page
jQuery makes AJAX super simple to use!
? Common jQuery AJAX Methods
echo "Hello from PHP!";
? In your HTML file
<button "#loadBtn").click(function() { $.get("data.php", function(data) { $("#result").html(data); }); });</script>
? This loads data from data.php when the button is clicked, and displays it in <div>.
? Example 2: $.post() (Send data to server)
? save.php
$name = $_POST['name'];echo "Hi, " . $name;?>
? HTML + jQuery
<input "#sendBtn").click(function() { var name = $("#nameInput").val(); $.post("save.php", { name: name }, function(response) { $("#response").html(response); }); });</script>
? Example 3: $.ajax() (Full Control)
$.ajax({ url: 'data.php', method: 'GET', success: function(data) { console.log("Success:", data); }, error: function(xhr, status, error) { console.error("Error:", error); }});
? Real-World Use Cases
Form submission without page reload
Live search or filtering
Loading comments or messages
Dependent dropdowns (country ? state ? city)
? Tip: Always Use event.preventDefault() in Forms
$("form").submit(function(e) { e.preventDefault(); // prevent normal form submission $.post("save.php", $(this).serialize(), function(response) { alert(response); });});