There is a simple chat on php / ajax / mysql. But unfortunately, when you submit a form, an empty string is entered into the database. What could be the problem?
Here is the file with the form, page.php:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#messages").load('ajaxLoad.php'); $("#userArea").submit(function(){ $.post('ajaxPost.php', $('#userArea').serialize(), function(data){ $("#messages").append('<div>'+data+'</div>'); }); return false; }); }); </script> <body> <div class="container"> <div id="messages"></div> <form method="post" action="ajaxPost.php" id="userArea" style="margin: 0 auto; font-size: 23px; text-align: center;"> <h1>Чат</h1> <input type="text" name="message" /> <input type="submit" value="ответить !" /> </form> </div> <?php include_once('ajaxLoad.php'); ?> <?php include_once('ajaxPost.php'); ?> The file that works with the form ajaxPost.php:
<?php include_once('page.php'); include('config.php'); $message = $_POST['message']; $db->Query("INSERT INTO messages(message1) VALUES ('$message')"); echo $message; ?>