I have an online chat that works with ajax and PHP . However, strangely, when I type only an interrogation ( ?
) and send the jQuery , it returns an error in the console. ajax enters error()
and the query ( ?
) is inserted in the database, but with strange characters type:
jQuery33106296323571939269_1534100132138.
JavaScript:
$('#envia_chat_id').keyup(function(e){
if(e.which === 13 & $('#envia_chat_id').attr('class') === 'envia_chat'){
if(trim($('.envia_chat').val()) != ''){
var msgenvio = $('.envia_chat').val();
var msgrender = msgenvio.replace(/</g, '<').replace(/>/g, '>');
$('#chat_box').append('<div class="msg user_' + $('.time_user').val() + '"><span>'+ $('.nick_user').val() + ': </span>'+ msgrender +'</div)');
$('.envia_chat').val('');
$('.envia_chat').removeAttr('class');
$('#chat_box').scrollTop($('#chat_box').prop("scrollHeight"));
$('#envia_chat_id').attr('placeholder','Aguarde para enviar outra...');
console.log('msg='+ msgenvio + '&type=enviaMsg&lobby=' + $('.lobbyid').val() + '&time=' + $('.time_user').val() + '&nick=' + $('.nick_user').val());
$.ajax({
url:'chat.php',
dataType: 'JSON',
type: 'POST',
data: 'msg='+ msgenvio + '&type=enviaMsg&lobby=' + $('.lobbyid').val() + '&time=' + $('.time_user').val() + '&nick=' + $('.nick_user').val(),
success: function(data){
console.log(data);
setTimeout(function(){
$('#envia_chat_id').attr('class','envia_chat');
$('#envia_chat_id').attr('placeholder','Chat...');
}, 4000);
},
error: function(data){
console.log(data);
$('#chat_box').append('<div class="msg msg_erro">Tente novamente enviar esta mensagem.</div)');
$('#chat_box').scrollTop($('#chat_box').prop("scrollHeight"));
$('#envia_chat_id').attr('class','envia_chat');
$('#envia_chat_id').attr('placeholder','Chat...');
}
});
}
}
});
PHP:
<?php
session_start();
function Erro($texto){
$array = array('status' => $texto);
return json_encode($array, JSON_PRETTY_PRINT);
}
function Sucesso($texto){
$array = array('status' => $texto);
return json_encode($array, JSON_PRETTY_PRINT);
}
if(isset($_SESSION['login_id']) and !empty($_SESSION['login_id']) and isset($_POST['type']) and isset($_POST['lobby']) and is_numeric($_POST['lobby']) == true){
require('connection.php');
$id = $_SESSION['login_id'];
$acao = $_POST['type'];
$id_lobby = (is_numeric($_POST['lobby'])==true) ? $_POST['lobby'] : NULL;
$tempo_atual = date("Y-m-d H:i:s");
$msg = (isset($_POST['msg'])==true and $_POST['msg'] != '') ? filter_var($_POST['msg'], FILTER_SANITIZE_SPECIAL_CHARS) : NULL;
$time = (isset($_POST['time']) == true and $_POST['time'] == 'azul' or $_POST['time'] == 'laranja' or $_POST['time'] == 'ambos') ? $_POST['time'] : NULL;
$nick = (isset($_POST['nick']) == true) ? $_POST['nick'] : NULL;
if(trim($msg) != NULL and $time != NULL and $nick != NULL){
$insere_msg = mysqli_query($conexao, "INSERT INTO chat (id_user, content, id_lobby, timestamp, time, nick) VALUES ('$id', '$msg', '$id_lobby', '$tempo_atual', '$time', '$nick')");
if($insere_msg === true){
echo Sucesso('success');
}
}else{
echo Erro('erro_null');
}
}else{
echo Erro('erro_faltam');
}
?>