-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
52 lines (48 loc) · 1.64 KB
/
index.php
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Chatbot</title>
<style>
#chatbox {
width: 500px;
height: 300px;
border: 1px solid #ccc;
padding: 10px;
overflow-y: scroll;
}
#userInput {
width: 500px;
}
#sendBtn {
padding: 5px 10px;
}
</style>
</head>
<body>
<div id="chatbox">
<div id="messages"></div>
</div>
<input type="text" id="userInput" placeholder="Type a message...">
<button id="sendBtn">Send</button>
<script>
document.getElementById('sendBtn').addEventListener('click', function() {
let userInput = document.getElementById('userInput').value;
let xhr = new XMLHttpRequest();
xhr.open('POST', 'chatbot.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = function () {
if (xhr.status === 200) {
let response = xhr.responseText;
let messages = document.getElementById('messages');
messages.innerHTML += '<p><strong>You:</strong> ' + userInput + '</p>';
messages.innerHTML += '<p><strong>Bot:</strong> ' + response + '</p>';
document.getElementById('chatbox').scrollTop = document.getElementById('chatbox').scrollHeight;
document.getElementById('userInput').value = '';
}
};
xhr.send('message=' + encodeURIComponent(userInput));
});
</script>
</body>
</html>