-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.php
111 lines (107 loc) · 3.02 KB
/
chat.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>We chat</title>
<link rel="shortcut icon" type="images/x-icon" href="favicon.png" />
<link rel="stylesheet" type="text/css" href="css/all.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<?php
define('chat',true);
session_start();
include_once 'php/config.php';
if(!isset($_SESSION['unique_id'])){
header('location:login.php');
}
$uniqueId = $_GET['user_id'];
$sql = mysqli_query($conn,"SELECT * from users where unique_id = {$uniqueId}");
$row = mysqli_fetch_assoc($sql);
?>
<section class="cht_sec">
<header class="cht_header">
<a href="user.php">
<div class="cht_h_icon">
<i class="fas fa-arrow-left"></i>
</div>
</a>
<img src="<?php echo "images/{$row['img']}"; ?>" alt="error loading
image" title="<?php echo $row['fname']. " " .$row['lname']; ?>"
draggable="false" />
<div class="cht_user">
<p><?php echo $row['fname']. " " .$row['lname']; ?></p>
<p><?php echo $row['status']; ?></p>
</div>
</header>
<div class="cht_box"></div>
<footer class="cht_foot">
<div class="cht_msg">
<form class="typing_area" method="POST">
<input
type="text"
name="outgoing_id"
value="<?php echo $_SESSION['unique_id']?>"
hidden
/>
<input
type="text"
name="incoming_id"
value="<?php echo $uniqueId?>"
hidden
/>
<input
type="text"
name="message"
placeholder="type a message here"
autofocus
/>
<button class="cht_send">
<i class="fab fa-telegram-plane"></i>
</button>
</form>
</div>
</footer>
</section>
<script type="text/javascript" src="js/all.js"></script>
<script type="text/javascript">
const form = document.querySelector('.typing_area');
const sendBtn = document.querySelector('.cht_send');
const chtArea = document.querySelector('.cht_box');
form.onsubmit = (e) => {
e.preventDefault();
};
sendBtn.onclick = () => {
let xhr = new XMLHttpRequest();
xhr.onload = () => {};
xhr.open('POST', 'php/insert-chat.php', true);
let formData = new FormData(form);
xhr.send(formData);
form.reset();
};
chtArea.onmouseenter = () => {
chtArea.classList.add('activa');
};
chtArea.onmouseleave = () => {
chtArea.classList.remove('activa');
};
setInterval(() => {
let xhr = new XMLHttpRequest();
xhr.onload = () => {
if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
let data = xhr.responseText;
chtArea.innerHTML = data;
if (!chtArea.classList.contains('activa')) {
chtArea.scrollTo(0, chtArea.scrollHeight);
}
}
};
xhr.open('POST', 'php/getdata.php');
let formData = new FormData(form);
xhr.send(formData);
}, 200);
</script>
</body>
</html>