-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (74 loc) · 2.88 KB
/
script.js
File metadata and controls
90 lines (74 loc) · 2.88 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
$(document).ready(function(){
let inputBoxData = {
name: "",
password: "",
chatbox: "",
otherName: ""
};
// Periodic AJAX request to list_names.php
var xhr1 = new XMLHttpRequest();
xhr1.open("GET", "listNames.php", true);
xhr1.onreadystatechange = function() {
if (xhr1.readyState == 4 && xhr1.status == 200) {
document.getElementById('userList').innerHTML = xhr1.responseText;
}
};
xhr1.send();
function sendMessage(data){
if (data !== undefined && data !== null) {
$.ajax({
type: "POST",
url: "serverside.php",
data: data,
success: function(response) {
console.log("Server response:", response);
},
error: function (error) {
console.error("AJAX error2:", error);
}
});
continuouslyFetchUpdates(document.getElementById("otherName"));
} else {
$("#warning").text("Insert Valid Username and Password.");
}
}
function updateServer(htmlID) {
$('#' + htmlID).on("keyup", function () {
// Check if the element with the specified ID exists
let $element = $(this);
if ($element.length) {
let inputVal = $element.val();
inputBoxData[htmlID] = inputVal;
clearTimeout($element.data('timer'));
$element.data('timer', setTimeout(function () {
sendMessage(inputBoxData);
}, 500));
} else {
console.error("Element with ID '" + htmlID + "' not found.");
}
});
}
// Function to continuously fetch updates from the database
// Function to continuously fetch updates from the database for a specific username
function continuouslyFetchUpdates() {
// Set an interval to fetch updates every 5 seconds
setInterval(function(){
var otherName = document.getElementById('otherName').value;
console.log(otherName);
var xhr = new XMLHttpRequest();
xhr.open("GET", "getMessages.php?otherName=" + otherName, true);
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var jsonResponse = JSON.parse(xhr.responseText);
document.getElementById('responseBox').innerHTML = jsonResponse.chatContent;
}
};
xhr.send();
}, 5000);
}
// Example: Call the function to start continuous fetching for a specific username
updateServer("name");
updateServer("password");
updateServer("chatbox");
// continuouslyFetchUpdates(document.getElementById("otherName"));
});