Skip to content

Commit 4afb9a3

Browse files
authored
Create script.js
1 parent 2fe6adc commit 4afb9a3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

script.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
document.addEventListener('DOMContentLoaded', () => {
2+
const chatForm = document.getElementById('chat-form');
3+
const chatInput = document.getElementById('chat-input');
4+
const chatOutput = document.getElementById('chat-output');
5+
6+
chatForm.addEventListener('submit', async (event) => {
7+
event.preventDefault();
8+
const message = chatInput.value.trim();
9+
10+
if (message.length === 0) return;
11+
12+
chatOutput.innerHTML += `<p class="user-message">${message}</p>`;
13+
chatInput.value = '';
14+
chatOutput.scrollTop = chatOutput.scrollHeight;
15+
16+
const response = await fetch('gptchat.php', {
17+
method: 'POST',
18+
headers: {
19+
'Content-Type': 'application/json',
20+
},
21+
body: JSON.stringify({ message }),
22+
});
23+
24+
if (response.ok) {
25+
const data = await response.json();
26+
chatOutput.innerHTML += `<p class="bot-message">${data.choices[0].text}</p>`;
27+
chatOutput.scrollTop = chatOutput.scrollHeight;
28+
} else {
29+
console.error('Error communicating with GPTChat API');
30+
}
31+
});
32+
});

0 commit comments

Comments
 (0)