Web Technology Unit- (4)
Web Technology Unit- (4)
1. Asynchronous communication: AJAX allows data exchange with the server in the background. The
user can continue interacting with the page while the data loads.
2. No page refresh: With AJAX, parts of the page can be updated without a full reload. This saves time
and bandwidth.
3. Faster response: AJAX makes websites faster because only small parts of the data are sent or received,
not the entire page.
4. Uses plain JavaScript and XMLHttpRequest: AJAX mostly uses the built-in XMLHttpRequest object
in JavaScript.
5. Can use multiple data formats: Although the name has "XML" in it, AJAX can work with XML,
JSON, HTML, or plain text.
How AJAX works:
Conclusion:
AJAX is a very useful technique that improves user experience by making websites fast and dynamic. It reduces
page reloads and helps in building real-time features using JavaScript and PHP. Even though new frameworks
are popular now, AJAX is still used in the background of many web applications
► Ajax Database
AJAX with Database using PHP:
AJAX is used to send and receive data from the server without reloading the page. When used with a database, it
helps fetch or update data in real-time.
How it works:
1. User types something or performs an action.
2. JavaScript sends an AJAX request to a PHP file.
3. PHP connects to the database and runs a query.
4. The result is sent back to JavaScript.
5. Page updates automatically, without refresh.
Example: Live Search
If we type a name in a search box, AJAX sends that name to PHP. PHP searches the database for matching
names and returns them. AJAX shows them live on the page.
Benefits:
No page reload
Faster user experience
Real-time results
So, AJAX with a database is useful for features like live search, real-time updates, auto-suggestions, and more.
JavaScript:
function searchUser() {
let input = document.getElementById("search").value;
let xhr = new XMLHttpRequest();
xhr.open("GET", "search.php?q=" + input, true);
xhr.onload = function() {
if (xhr.status == 200) {
document.getElementById("result").innerHTML = xhr.responseText;
}
};
xhr.send();
}
PHP (search.php):
$q = $_GET['q'];
$conn = mysqli_connect("localhost", "root", "", "mydb");
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo $row['name'] . "<br>";
}
} else {
echo "No results found";
}
AJAX is widely used in modern web development. Some common uses are:
<div id="response"></div>
<script>
function submitForm() {
let name = document.getElementById("name").value;
let email = document.getElementById("email").value;
xhr.onload = function () {
document.getElementById("response").innerHTML = xhr.responseText;
};
PHP (submit.php):
<?php
$name = $_POST['name'];
$email = $_POST['email'];
How it Works:
1. AJAX Request is triggered from JavaScript (for example, a user action like typing or clicking a button).
2. The AJAX request sends data (like a search query or form input) to a PHP file.
3. PHP connects to the MySQL database and performs a query (e.g., SELECT, INSERT, UPDATE).
4. The PHP file returns the result (like new records or confirmation) to the JavaScript.
5. JavaScript dynamically updates the webpage using the received data, without needing a page reload.
Example: AJAX PHP MySQL Search (Live Search)
This is a common example where we use AJAX with PHP and MySQL to search for data (e.g., users' names)
from a database.
<script>
function searchUser() {
let input = document.getElementById("search").value;
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo $row['name'] . "<br>";
}
} else {
echo "No results found.";
}
?>
MySQL Table Setup:
CREATE DATABASE userdb;
USE userdb;
► Q: Explain how to connect to a MySQL database using PHP. Create a database, create a table,
insert data, and run a SELECT query.
Conclusion:
Connect: Use mysqli_connect() to connect to the MySQL server.
Create Database: Use CREATE DATABASE to create a new database.
Create Table: Define table structure and use CREATE TABLE SQL query.
Insert Data: Use INSERT INTO to add data to the table.
Select Data: Use SELECT to retrieve data from the table.
This is how we interact with a MySQL database using PHP—connecting, creating databases and tables,
inserting, and fetching data.
3. Write a PHP script to connect to a MySQL database and fetch data using AJAX.
Expected Answer:
HTML + JavaScript (index.html):
<input type="text" id="search" onkeyup="searchUser()">
<div id="result"></div>
<script>
function searchUser() {
let input = document.getElementById("search").value;
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo $row['name'] . "<br>";
}
} else {
echo "No results found.";
}
?>
MySQL Table Setup:
CREATE DATABASE userdb;
USE userdb;
<div id="response"></div>
<script>
function submitForm() {
let name = document.getElementById("name").value;
let email = document.getElementById("email").value;
xhr.onload = function () {
document.getElementById("response").innerHTML = xhr.responseText;
};
<?php
$name = $_POST['name'];
$email = $_POST['email'];
USE formdb;
7. What are the key differences between synchronous and asynchronous requests in AJAX?
Expected Answer: