Пытаюсь сделать небольшой чатик, не отправляется данные с input, не могу понять в чем дело. Консоль дает пустую строку.
Сама форма:
<form class="chat">
<div class="input-group">
<input id="chat_text" class="form-control" placeholder="Введите сообщение" name="chat_text"/>
<span class="input-group-btn">
<button type="submit" class="btn btn-primary" name="chat_message_send" id="chat_message_send">Отправить</button>
</span>
</div>
</form>
AJAX
$('.chat').submit(function (e) {
e.preventDefault(); // avoid to execute the actual submit of the form.
var message = document.getElementById('chat_text').value;
$('#chat_text').val('');
$.ajax({
type: "POST",
url: hdir + "actions/responderActions.php",
data: {
'chat_message_send': 'yes',
'chat_text': message
},
success: function (response) {
console.log(response);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log("Error");
}
});
});
PHP обработчик
function chat_message_send() {
$chat_message_text = htmlspecialchars($_POST['chat_text']);
session_start();
$uid = $_SESSION['id'];
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
mysqli_set_charset($link, 'utf8');
if (!$link) {
die('Could not connect: ' .mysql_error());
}
$sql = "INSERT INTO chat (userId, chat_text) VALUES (?, ?)";
try {
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, "is", $uid, $chat_message_text);
$result = mysqli_stmt_execute($stmt);
if ($result == FALSE) {
die(mysqli_error($link));
}
}
catch (Exception $e)
{
die("Failed to run query: " . $e->getMessage()); //TODO: A function to send me an email when this occurs should be made
}
mysqli_close($link);
}