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>'; ?> ?>
$("form").submit();
- DaemonHK