From the form it is necessary to send data to the database and display it on the screen. The program for some reason reads empty values ​​from input / textarea . Null values ​​are stored in the database. I can not find an error. Thank you in advance

index.php

 <form method="post"> <div class="row justify-content-start"> <div class="col-lg-10"> <input rows="1" cols="25" name="inputMessage" class="form-control" id="inputMessage" placeholder="Enter yout message" required> </div> <div class="col-lg-1"> <button type ="submit" id="submit" name="submit" class="btn btn-primary"><i class="fas fa-chevron-right"></i></button> </div> </div> </form> 

main.js - here ajax request

 $(function() { $('#submit').click(function(){ var inputMessage = $('#inputMessage').val(); console.log(inputMessage); $.ajax({ type: 'post', url: "messages.php", data: "inputMessage=" + inputMessage, success: function(response) { alert("form was submitted"); $('.perepiska-content').html(response); } }); }); return false; }); 

messages.php - Inserting into the database and output to the screen

 <?php <?php $db_host='localhost'; $db_user='root'; $db_pass=''; $db_database='db_shop'; $link=mysql_connect($db_host, $db_user, $db_pass); mysql_select_db($db_database, $link) or die("No Connection to Database".mysql_error()); mysql_query("SET names latin1"); $inputMessage=$_POST['inputMessage']; //question $date = date('Ymd H:i:s'); //date of question $query1="INSERT INTO chat_questions (question, date_question) VALUES ('$inputMessage', '$date')"; $result1=mysql_query($query1, $link); echo ' <div class="showQuestion"> <div class="showQuestionContent"> </div>'; ?> ?> 
  • Do not click on the button to process, and $("form").submit(); - DaemonHK

1 answer 1

Why do you need "SET names latin1"?
mysql_query is deprecated somewhere in php 5.5, it is worth using mysqli

 <form method="post" action="messages.php"> <div class="row justify-content-start"> <div class="col-lg-10"> <input rows="1" cols="25" name="inputMessage" class="form-control" id="inputMessage" placeholder="Enter yout message" required> </div> <div class="col-lg-1"> <button type ="submit" id="submit" name="submit" class="btn btn-primary"><i class="fas fa-chevron-right"></i></button> </div> </div> </form> <?php require_once('db.php'); if(isset($_POST['inputMessage'])) { $inputMessage = $_POST(['inputMessage']); $date = date('Ymd H:i:s'); $SQL = "INSERT INTO chat_questions (question, date_question) VALUES ('$inputMessage', '$date')"; $result = mysqli_query($link, $SQL); if ($result) { header('Location:index.php'); } else { printf("Ошибка: %s\n", mysqli_error($link)); } } ?> 

File db.php:

  <?php //Параметры подключения в БД $host = "localhost"; //имя хоста $database = ""; //имя базы $user = ""; //пользователь $password = ""; //пароль $dbtable = ""; //таблица $link = mysqli_connect($host, $user, $password, $database) or die("Ошибка " . mysqli_error($link)); ?>