-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
37 lines (32 loc) · 1.1 KB
/
Copy pathscript.js
File metadata and controls
37 lines (32 loc) · 1.1 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
async function loadHistory() {
const res = await fetch("chatbot.php");
const chat = await res.json();
chat.forEach(msg => {
appendMessage("user", msg.user);
appendMessage("bot", msg.bot);
});
}
function appendMessage(sender, text) {
const messagesDiv = document.getElementById("messages");
const msg = document.createElement("div");
msg.className = "message " + sender;
msg.innerText = text;
messagesDiv.appendChild(msg);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
function sendMessage() {
let input = document.getElementById("userInput");
let message = input.value.trim();
if (message === "") return;
appendMessage("user", message);
input.value = "";
fetch("chatbot.php", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: "message=" + encodeURIComponent(message)
})
.then(res => res.text())
.then(reply => appendMessage("bot", reply))
.catch(() => appendMessage("bot", "Error connecting to server."));
}
loadHistory();