File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments