没有在索引页面中显示消息

时间:2017-09-12 11:26:36

标签: php mysql ajax

我正在尝试使用php,mysql和ajax创建小型聊天应用程序。使用ajax函数后,消息未显示在输出中。但是数据会传输到数据库。

索引页码

<?php

  require('includes/core.inc.php');

  if(isset($_POST['send'])){

    if(send_msg($_POST['sender'], $_POST['message'])){
      echo 'Message sent.';
    }else{
      echo 'message faild to sent.';
    }

  }
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <!--Page title-->
    <title>chat application</title>

    <!--css stylesheet-->
    <link type="text/css" rel="stylesheet" href="public/css/main.css"/>
  </head>
  <body>

    <div id="input">
      <form action="index.php" method="post">
        <lable>Enter name:<input type="text" name="sender"/></lable>
        <lable>Enter message:<input type="text" name="message"/></lable><br/>
        <input type="submit" name="send" value="send message"/>
      </form>
    </div><!--input-->


    <div id="messages">

    </div> <!--Messages-->

<!--JAVASCRIPT-->

  <script type="text/javascript" src="scripts/js/jquery-1.7.2.min.js"></script>
  <script type="text/javascript" src="scripts/js/auto_chat.js"></script>


  </body>
</html>

chat.php

<?php
  require('../../includes/database/connect.db.php');
  require('../../includes/functions/chat.func.php');

  $messages = get_msg();
  foreach($messages as $message){
    echo '<strong>'.$message['sender'].' sent</strong><br/>';
    echo $message['message'].'<br/><br/>';
  }

 ?>

聊天功能

<?php

  function get_msg(){

    $query = "SELECT Sender,Message FROM chat";

    $run = mysql_query($query);

    $messages = array();


    if($run === FALSE) {
      die(mysql_error()); // TODO: better error handling
    }


    while($message = mysql_fetch_assoc($run)){

      $messages[] = array('sender'=>$message['Sender'], 'message'=>$message['Message']);

    }

    return $messages;

  }

  function send_msg($sender, $message){
    if(!empty($sender) && !empty($message)){
      $sender = mysql_real_escape_string($sender);
      $message = mysql_real_escape_string($message);

      $query = "INSERT INTO chat VALUES (null , '{$sender}', '{$message}')";

      if($run = mysql_query($query)){
        return true;
      }else{
        return false;
      }

    }else{
      return false;
    }

  }

?>

我添加的ajax部分就是这个。我认为这是问题所在,但我无法找到它。我之前没有使用过jquery和ajax。那么有任何先决条件吗?

$(document).ready(function() {
  var interval = setInterval(function() {
    $.ajax({
      url: 'scripts/php/chat.php',
      success: function(data){
        $('#messages').html(data);
      }
    });
  }, 1000);
});

我有数据库连接文件。但没有问题。如果想要我可以发布它。请帮帮我

1 个答案:

答案 0 :(得分:1)

将下面的代码放在index.php文件

<?php
error_reporting(0);
$conn = mysql_connect('localhost','root','');
$db = mysql_select_db('test',$conn);
  function send_msg($sender, $message){
    if(!empty($sender) && !empty($message)){
      $sender = mysql_real_escape_string($sender);
      $message = mysql_real_escape_string($message);
      $query = "INSERT INTO chat VALUES (null , '{$sender}', '{$message}')";
      if($run = mysql_query($query)){
        return true;
      }else{
        return false;
      }
    }else{
      return false;
    }
  }
  if(isset($_POST['send'])){
    if(send_msg($_POST['sender'], $_POST['message'])){
      echo 'Message sent.';
    }else{
      echo 'message faild to sent.';
    }
  }
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <!--Page title-->
    <title>chat application</title>
    <!--css stylesheet-->
    <link type="text/css" rel="stylesheet" href="public/css/main.css"/>
  </head>
  <body>
    <div id="input">
      <form action="" method="post">
        <lable>Enter name:<input type="text" name="sender"/></lable>
        <lable>Enter message:<input type="text" name="message"/></lable><br/>
        <input type="submit" name="send" value="send message"/>
      </form>
    </div><!--input-->
    <div id="messages">
    </div> <!--Messages-->
  </body>
</html>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script>
$(document).ready(function() {
  var interval = setInterval(function() {
    $.ajax({
      url: 'test1.php',
      success: function(data){
        $('#messages').html(data);
      }
    });
  }, 1000);
});
</script>

为ajax call test1.php创建一个文件,并将其放在代码

下面
<?php
error_reporting(0);
$conn = mysql_connect('localhost','root','');
$db = mysql_select_db('test',$conn);
  function get_msg(){
    $query = "SELECT Sender,Message FROM chat";
    $run = mysql_query($query);
    $messages = array();

    if($run === FALSE) {
      die(mysql_error()); // TODO: better error handling
    }

    while($message = mysql_fetch_assoc($run)){
      $messages[] = array('sender'=>$message['Sender'], 'message'=>$message['Message']);
    }
    return $messages;
  }


  $messages = get_msg();
  foreach($messages as $message){
    echo '<strong>'.$message['sender'].' sent</strong><br/>';
    echo $message['message'].'<br/><br/>';
  }

 ?>
以上

对我有用。不需要任何其他js文件。