0
Good night,
I have a little chat, in html, php and jquery, I just need to send the message form without button, but it’s just not working, someone could help me is so the code:
<form methood='post' id="message-form">
<textarea class="textarea" id="message_text" placeholder="Write your message"></textarea>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="sub_file/jquery-3.1.1.min.js"></script>
<script>
$("document").ready(function(event){
// agora, se o formulário for enviado
$("#right-col-container").on('submit', '#message-form' function(){
// pega os dados da textarea
var message_text = $("#message_text").val();
// envia os dados para o arquivo sending_process.php
$.post("sub_file/sending_process.php?user=<?php echo $_GET['user'];?>",
{
text: message_text,
},
//in return we'll get
function(data, status){
// primeiro remova o texto de
// message_text so
$("#message_text").val("");
// agora adicione os dados dentro
// o contêiner de mensagens
document.getElementById("messages-container").innerHTML += data;
}
);
});
// se algum botão for clicado dentro
//right-col-container
$("#right-col-container").keyup(function(e){
// como vamos enviar o formulário com o botão enter
if(e.keyCode == 13){
// então envie o formulário
$("#message-form").submit() ;
}
})
});
</script>
my file I do the sending_process form Insert:
<?php
session_start() ;
require_once("../connection.php");
if(isset($_SESSION['username']) and isset($_GET['user'])){
if(isset($_POST['text'])){
//now check for empty message
if($_POST['text'] !=''){
//now from here we can insert
//data into the database
$sender_name = $_SESSION['username'];
$reciever_name = $_GET['user'];
$message = $_POST['text'] ;
$date = date("Y-m-d h:i:sa") ;
$q = "INSERT INTO messages
(sender_name, reciever_name, message_text, date_time)
VALUES('$sender_name','$reciever_name','$message', '$date')";
$r = mysqli_query($con, $q) ;
if($r){
//message sent
?>
<div class="grey-message">
<a href="#">Me</a>
<p><?php echo $message; ?></p>
</div>
<?php
}else{
//problem in query
echo $q;
}
}else{
echo 'please write something first' ;
}
}else{
echo 'problem with text';
}
}else{
echo 'please login or select a user to send a message';
}
?>
tried but didn’t work buddy, still no send.
– Ragnar
I can see your code?
– Assis Duarte