-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
115 lines (76 loc) · 2.91 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
let message_sent_id=[];
var isalpha = /^[A-Za-z]+$/;
window.onload=()=>{
document.querySelector("#message").value = "";
document.querySelector("#set_name_input").value="";
let x=document.querySelector("#set_name_input");
if(localStorage.getItem("user")){
x.value=localStorage.getItem("user");
x.disabled=true;
}
}
document.querySelector("#set_name_input").addEventListener("input",()=>{
let x=document.querySelector("#set_name_input");
if(!String(x.value).slice(-1).match(isalpha))
x.value=String(x.value).slice(0,-1);
document.querySelector("#case_number").innerHTML="12"-String(x.value).length;
})
document.querySelector("#set_name").addEventListener("click",()=>{
let x=document.querySelector("#set_name_input");
if(x.value==""){
document.querySelector("#error_message").innerHTML = "name can t be empty";
return
}
localStorage.setItem("user",x.value);
x.disabled=true;
})
document.querySelector("#update_name").addEventListener("click",()=>{
let x=document.querySelector("#set_name_input");
x.disabled=false;
})
document.querySelector("#send_button").addEventListener("click", async () => {
if (document.querySelector("#message").value == "") {
document.querySelector("#error_message").innerHTML = "empty field";
return;
}
let last_message_id_query=await fetch("display.php");
let last_messge_id=await last_message_id_query.json()
message_sent_id.push(Number(last_messge_id.msg[0][0])+1);
let request = await fetch("insert.php", {
method: "post",
body: new FormData(document.forms[0]),
});
let resualt = await request.json();
if (resualt.msg == "send") {
document.querySelector("#message").value = "";
document.querySelector("#error_message").innerHTML = "";
}
else
document.querySelector("#error_message").innerHTML = "message not sent";
// console.log(resualt);
});
let handel=setInterval(async()=>{
let request = await fetch("display.php");
let resualt = await request.json();
let table=document.querySelector("#message_table");
table.innerHTML=null
for(let i=9;i>=0;i--){
var tr=document.createElement("tr")
tr.setAttribute("class","table_row")
var td=document.createElement("td")
text=document.createTextNode(resualt.msg[i][1]);
td.appendChild(text)
tr.appendChild(td)
table.appendChild(tr)
if(message_sent_id.indexOf(Number(resualt.msg[i][0]))!=-1){
tr.setAttribute("style","position: relative;left:65px")
}
else{
tr.setAttribute("style","position: relative;left:-65px")
}
/*console.log(resualt.let x=document.querySelector("#set_name_input");msg[i][1]);
console.log(message_sent_id);*/
}
// console.log(resualt.msg[0][1]);
},1000);
//setTimeout(clearInterval(handel),5000)