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
3 Answers
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>
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
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,