Email Finder Example - (AJAX Examples)
In this tutorial, we'll walk through an example of an email finder built using AJAX. AJAX is a technique used to build asynchronous web applications where data can be requested from a server without refreshing the entire page. Our email finder will allow the user to enter a name and retrieve the email address associated with that name using AJAX.
Syntax
The syntax for our AJAX email finder will consist of a form that includes an input field for the user to enter a name, a button to submit the form, and a section to display the resulting email address.
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<button type="button" onclick="findEmail()">Find Email</button>
</form>
<div id="result"></div>
The onlick
attribute of the button element will trigger a JavaScript function named findEmail()
. The result of the email address lookup will be displayed in a div element with an id of result
.
Example
Let's see how our AJAX email finder works with a simple example. We'll assume that we have an endpoint in our server that takes a name as a query parameter and returns the associated email address.
function findEmail() {
const name = document.getElementById('name').value;
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
const result = document.getElementById('result');
result.innerHTML = xhr.responseText;
}
};
xhr.open('GET', `findemail.php?name=${name}`, true);
xhr.send();
}
When the findEmail()
function is invoked, it gets the value of the name input field. It then creates a new instance of the XMLHttpRequest object, which is used to make the request to the server. The onreadystatechange
function is triggered every time the xhr object's state changes.
When the xhr object's state is 4
(done) and the status is 200
(OK), the function retrieves the email address from the xhr object's response text and displays it in the result
div.
Explanation
Our AJAX email finder works by sending a request to our server using the XMLHttpRequest object. We pass the name that the user entered as a query parameter to the server and wait for the response. When the response is received, we extract the email address from it and display it in the result
div.
Use
This AJAX email finder can be used to retrieve email addresses associated with names. It can be used on websites, web applications or as a part of a larger AJAX application.
Important Points
- AJAX allows us to retrieve data from the server without refreshing the entire page.
- The XMLHttpRequest object is used to make AJAX requests.
- The
onreadystatechange
function is triggered every time the xhr object's state changes. - The
readyState
property of the xhr object has a value of4
when the request is complete. - The
status
property of the xhr object has a value of200
when the request is successful.
Summary
In this tutorial, we built an AJAX email finder that allows the user to enter a name and retrieve the associated email address. We covered the syntax, example, output, explanation, use, important points, and summary of this AJAX email finder. With this knowledge, you can now build your own AJAX applications that retrieve data from servers without refreshing the entire page.