I'm trying to minimize to the maximum how very small I'll post my whole code, I came across a problem the code is working perfectly, but when I click on the send button it works, I'd like to know how to make it work by pressing the enter key or clicking.OBS: I do not handle much of 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 file named chatlog.htm