-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.js
51 lines (47 loc) · 1.39 KB
/
users.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const searchBar = document.querySelector(".search input"),
searchIcon = document.querySelector(".search button"),
usersList = document.querySelector(".users-list");
searchIcon.onclick = ()=>{
searchBar.classList.toggle("show");
searchIcon.classList.toggle("active");
searchBar.focus();
if(searchBar.classList.contains("active")){
searchBar.value = "";
searchBar.classList.remove("active");
}
}
searchBar.onkeyup = ()=>{
let searchTerm = searchBar.value;
if(searchTerm != ""){
searchBar.classList.add("active");
}else{
searchBar.classList.remove("active");
}
let xhr = new XMLHttpRequest();
xhr.open("POST", "php/search.php", true);
xhr.onload = ()=>{
if(xhr.readyState === XMLHttpRequest.DONE){
if(xhr.status === 200){
let data = xhr.response;
usersList.innerHTML = data;
}
}
}
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("searchTerm=" + searchTerm);
}
setInterval(() =>{
let xhr = new XMLHttpRequest();
xhr.open("GET", "php/users.php", true);
xhr.onload = ()=>{
if(xhr.readyState === XMLHttpRequest.DONE){
if(xhr.status === 200){
let data = xhr.response;
if(!searchBar.classList.contains("active")){
usersList.innerHTML = data;
}
}
}
}
xhr.send();
}, 500);