1
Well I am trying to minimize to the maximum as this very small I will post my entire code, I came across a problem the code is working perfectly, but only when I click the send button that works, I would like to know how to make it work by pressing the enter key or clicking.NOTE:I don’t know much about jquery
index.html
<div id="chat"></div><br />
<form action="#" method="post">
<input type="hidden" id="nome" value="<?=$usuario?>" size="6">
<input type="text" id="mensagem" width="80%" >
<button type="button" id="enviar" title="enviar" >Enter</button>
</form>
chat php.
<?php
$nome = $_POST['nome'];
$mensagem = $_POST['mensagem'];
$linha = $nome.' - '.$mensagem.'<br>';
$arquivo = file('chatlog.htm');
array_unshift($arquivo, $linha);
file_put_contents('chatlog.htm', $arquivo);
?>
script
$(function() {
conversar();
$('#enviar').click(function() {
var nome = $('#nome').val();
var mensagem = $('#mensagem').val();
$.post('chat.php', {'nome':nome, 'mensagem': mensagem }, function() {
conversar;
$('#mensagem').val('');
});
});
function conversar() {
$('#chat').load('chatlog.htm');
$('#mensagem').focus();
}
setInterval(conversar, 3000);
});
css
#chat {
width: 300px;
height: 400px;
border: black 1px solid;
background-color: #000000;
overflow-x: hidden;
overflow-y: scroll;
padding: 5px;
margin-left: 10px;
}
fieldset {
width: 330px;
background-color: #e6e2af;
border: black 1px solid;
}
data is saved in a chatlog.htm filename
Instead of type button try type Submit, Chrome ran cool <button type="Submit" id="send" title="send" >Enter</button>``
– user60252