-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
59 lines (50 loc) · 1.57 KB
/
script.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
52
53
54
55
56
57
58
59
const chatForm = document.getElementById("chat-form");
const chatInput = document.getElementById("chat-input");
const chatOutput = document.getElementById("chat-output");
chatForm.addEventListener("submit", async (e) => {
e.preventDefault();
const message = chatInput.value.trim();
if (!message) return;
displayUserMessage(message);
try {
const response = await fetch("gptchat.php", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ message }),
});
if (response.ok) {
const data = await response.json();
if (data.message) {
displayBotMessage(data.message);
} else {
console.error("Error: Unexpected response format", data);
}
} else {
console.error("Error communicating with GPTChat API");
}
} catch (error) {
console.error("Fetch error:", error);
}
// Clear input field
chatInput.value = "";
});
// Function to display user message in chat
function displayUserMessage(message) {
chatOutput.innerHTML += `
<div class="user-message speech-bubble">
${message}
</div>
`;
chatOutput.scrollTop = chatOutput.scrollHeight;
}
// Function to display bot message in chat
function displayBotMessage(message) {
chatOutput.innerHTML += `
<div class="bot-message speech-bubble">
${message}
</div>
`;
chatOutput.scrollTop = chatOutput.scrollHeight;
}