Website chat

How to create a chat? Need a code. Please help, I need a field: name, message text.


Answer 1, authority 100%

Now I remember myself =)
So we do something like this:

  1. Create table Messages
  2. We create 3 columns in it
  3. The first column is id, the second is usernameand the third is time
  4. Create two files chat.phpand iframe.php
  5. In the file chat.phpput this:

Code:

<script>
    jQuery(function($) {
        $("#form").onsubmit(function (event) {
            var message = $("input[name='message']", form).value()
            if (message) { event.preventDefault(); return false; }
            return true;
        });
        $("#form input[name='message']").bind(
            'change keydown keypress',
            function(event) {
                var text = $(this).value().replace(/^\s+/, "").replace(/\s+$/, "");
                $("#form input[type='submit']").prop("disabled", text == "");
            }
        );
    });
  </script>
  <html>
  <head>
  </head>
  <body>
  <iframe name='chatWindow' id='chatWindow' src ="iframe.php" width="850" height="400"></iframe>
  <form action="iframe.php" method="post" id="form" target='chatWindow' > 
  </body>
</html>
  • And in iframe.php we put this:

Code:

<?php
  session_start();
  if (isset($_POST['message'])) {
      $message = $_POST['message'];
  }
  $db = mysql_connect("", "", "");
  mysql_select_db("", $db);
  if (trim($message) != "") {
      $time = time();
      mysql_query("INSERT INTO messages (message,username,time) VALUES ('$message','{$_SESSION['username']}','$time')");
  }
  $result = mysql_query("SELECT * FROM messages");
  while ($row = mysql_fetch_array($result)) {
      echo "<br>";
      echo "[".date(' H:i ',$row['time'])."]".$row['username']."  : ".$row['message'];
  }
?>

Rejoice everyone. If anything, knock on Skype komar12124.