Quantcast
Channel: Tópicos
Viewing all articles
Browse latest Browse all 14700

Insere da DB mas não mostra o que inseri

$
0
0
como podem ver isto pede o username, escrevo o nome e clico enter, ele insere na BD. Depois a parte da mensagem, primeiro deveria aparecer a mensagem No chats found. Be the first e não aparece, e depois mesmo que eu escreva uma mansagem e a envie ela é adicionada à DB mas não me mostra o que inseri
index.php
Código (HTML):
<?php
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>Ajax Chat</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<style type="text/css">
  #chatbox {
   background-color: #DDD;
   border: 1px solid #000;
   width: 600px;
   height: 300px;
  }
  #chatbox #initial {
   text-align: center;
   margin: auto;
   width: 250px;
   padding-top: 100px;
  }

  #chatbox #primary {
   display: none;
  }
  #chatbox #primary #window {
   width: 100%;
   height: 265px;
   background-color: #FFF;
   border-bottom: 1px solid #AAA;
   overflow: scroll;
  }
  #chatbox #primary #window .list1 {
   background-color: #EEE;
   padding: 5px;
   border-bottom: 1px solid #AAA;
  }
  #chatbox #primary #window .list2 {
   background-color: #DDD;
   padding: 5px;
   border-bottom: 1px solid #AAA;
  }
  #chatbox #primary #form {
   width: 100%;
  }
</style>
<script type="text/javascript">
  var updateTime = 5;
  var running = false;
  var counter = 0;
  function chat_initial() {
   var user = document.getElementById("chat_user").value;
   $.post('./chat.php', {stage:"initial", user:user}, function(data) {
        if (data == "good") {
         $('#chatbox #initial').css('display', 'none');
         $('#chatbox #primary').css('display', 'inline');
        }
         else
         alert("The username is taken. Please try another!");
   });
  }
  function chat_update() {
   if (counter == updateTime)
        chat_load();
   else
        counter ++;
   if (running == true)
        setTimeout("chat_update();", 1000);
  }
  function chat_load() {
   $('#chatbox #primary #window').html(data);
   $.post('./chat.php', {stage:"load"}, function(data){
        $('#chatbox #primary #window').html(data);
        counter = 0;
        setTimeout("chat_update();", 1000*updateTime);
   });
  }
  function chat_send() {
   var text = document.getElementById("chat_text").value;
   $.post('./chat.php', {stage:"send", text:text}, function(data) {
        document.getElementById("chat_text").value = '';
        if (data == "good") {
         chat_load();
        }
        else
         alert("No username was found. Please. Please reload the chat window");
   });
  }
</script>
</head>
<body>
<div id="chatbox">
  <div id="initial">
   <table>
        <tr align="center">
         <td>Enter a username</td>
        </tr>
        <tr align="center">
         <td><input type="text" name="chat_user" id="chat_user" style="width: 250px;" onkeypress="if(event.keyCode == 13){ chat_initial(); }" /></td>
        </tr>
        <tr align="center">
         <td><input type="button" value="Continue" onclick="chat_initial();"></td>
        </tr>
   </table>
  </div>
  <div id="primary">
   <div id="window"></div>
   <div id="form">
        <table style="width: 100%;">
         <tr>
          <td width="90%"><input type="text" name="chat_text" id="chat_text" style="width: 100%;" onkeypress="if(event.keyCode == 13){ chat_send(); }" /></td>
          <td align="center"><input type="button" id="chat_send" value="Chat!" onclick="chat_send();"/></td>
         </tr>
        </table>
   </div>
  </div>
</div>
</body>
</html>

chat.php
Código (PHP):
<?php
//
session_start();
// Connect to the database
mysql_connect('127.0.0.1', 'root', '');
mysql_select_db('tutorials');
// Reads the stage
$stage = $_POST['stage'];
// primary code
if ($stage == "initial") {
  // check the username
  $user = $_POST['user'];
  $query = mysql_query("SELECT * FROM chat_active WHERE user='$user'");
  if(mysql_num_rows($query) == 0){
   $time = time();
   //
   mysql_query("INSERT INTO chat_active VALUES ('$user', '$time')");
   // set the session
   $_SESSION['user'] = $user;
   echo 'good';
  }
  else
   echo 'taken';
}
else if ($stage == "send") {
  // get the text
  $text = $_POST['text'];
  // check for a user
  if (isset($_SESSION['user'])) {
   $user = $_SESSION['user'];
   $time = time();
   mysql_query("INSERT INTO chat_chats VALUES ('$user', '$time', '$text')");
   echo 'good';
  }
  else
   echo 'no user';
}
else if($stage == "load"){
  $num = 1;

  $query = mysql_query("SELECT * FROM chat_chats ORDER BY time DESC");
  if (mysql_num_rows($query) > 0) {
   while ($row = mysql_fetch_assoc($query)) {
        $user = $row['user'];
        $time = $row['time'];
        $content = $row['content'];
        $content = htmlentities($content);
        echo '<div class="list'.$num.'">';
         echo '<b>'. $user.'</b> at <i>'.date("M d, Y", $time).'</i> - '.$content;
        echo '</div>';
        if ($num = 1)
         $num = 2;
        else
         $num = 1;
   }
  }
  else
   echo 'No chats found. Be the first';
}
else
  echo 'error';
?>

Viewing all articles
Browse latest Browse all 14700