0

chat on php

I have a code, that can show messages, but it show message only when i reload page. How can i fix it? code:<?php if (session_id() == ""){ session_start(); } ?> <script src = "http://code.jquery.com/jquery-3.1.1.js"></script> <script type = " text/javascript"> window.onload = function(){ document.getElementById('block').scrollTop = 9999; } </script> <div id = "block" style = "border: 1px solid; overflow: scroll; width: 800px; height: 300px;"> <?php $fldata = file("chat.txt"); f

26th Jan 2017, 9:52 AM
Жадный Кот
Жадный Кот - avatar
3 Antworten
0
all code<?php if (session_id() == ""){ session_start(); } ?> <script src = "http://code.jquery.com/jquery-3.1.1.js"></script> <script type = " text/javascript"> window.onload = function(){ document.getElementById('block').scrollTop = 9999; } </script> <div id = "block" style = "border: 1px solid; overflow: scroll; width: 800px; height: 300px;"> <?php $fldata = file("chat.txt"); foreach ($fldata as $line){ echo $line; } ?> </div> <?php $us = $_SESSION['username']; if (isset($_POST['but'])){ $file = fopen("chat.txt","a+"); $messgg = $_POST['messg']; $txt = "<div style = ' border: 1px solid #dddddd; background: #f2f2f2; padding: 10px; color: #000000; margin: 10px '><b>".$us.":</b> ".$messgg."<br></div>"; fwrite($file,$txt); fclose($file); } ?> <form action = "./chat.php" method = "post"> <textarea style = "resize: none;" name = "messg"></textarea><br> <input type = "submit" name = "but" value = "Отправить"> </form>
26th Jan 2017, 9:53 AM
Жадный Кот
Жадный Кот - avatar
0
because PHP is backend technology, so client will download data from server after he reloads the page. To fix it I'd recommend you using some of javascript backend frameworks, like node.js or socket.io
26th Jan 2017, 10:17 AM
Jakub Stasiak
Jakub Stasiak - avatar
0
node and socket are not needed for what's happening here. Have the PHP pull the chat in a separate callable function via AJAX or jQuery POST. Put the Javascript request on interval and it should call the function repeatedly at desired increment,
26th Jan 2017, 11:11 AM
Louis Milotte
Louis Milotte - avatar