0

我正在为一个网站编写一个实时聊天功能,除了显示输入的文本外,我一切正常,我可以让我的默认语句显示在聊天框中,但是我必须有一个完全可以做到这一点的功能这里的单独页面是功能

    if(isset($_POST['method']) === true && empty($_POST['method']) === false)
    {
        $method = trim($_POST['method']);

        if($method === 'fetch')
        {
            $messages = fetchMessages();

            if(empty($messages) === true)
            {
                echo 'A representative will be with you shortly';
            }else
            {
                foreach($messages as $message)
                {
                    $ts = $message['timestamp'];
                    ?>
                    <div class = "message">
                        <a href = "#"><?php echo date('n-j-Y h:i:s a', $ts); ?>
                        <?php echo $message['username']; ?></a>says:<p>
                        <?php echo nl2br($message['message']); ?></p>
                    </div>
                    <?php
                }
            }
        }
    }

我当然是在调用fetchMessage()这个文件中的函数。但是,要使该功能正常工作,我必须将它放在 chatRoom.php 文件中,以便我可以将会话 ID 传递给它,以便它仅获取引用该特定会话的聊天。

如果我将此脚本移动到 chatRoom.php 文件或在此文件中包含 chatRoom.php 文件,聊天室会中断并在应该进行聊天的聊天框中显示其自身的第二个副本,并且不会执行任何操作。

如果我使用此代码将 fetchMessage 移动到文件中,我只会得到默认消息,不会显示任何其他消息。

我正在使用 jquery 和 ajax 对聊天进行增量获取,该聊天存储在我数据库的表中。如果您需要任何其他信息,请询问我很乐意添加此信息,但不确定需要什么。我对 jquery 和 ajax 还是很陌生。

好的,这里是 chatRoom.php 的代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <?php
    session_start();
    include 'conection.php';

    $name = $_GET['name'];
    $query = "SELECT * FROM chatSession WHERE user_name = '$name'";
    $result = mysql_query($query, $con) or die(mysql_error());
    $row = mysql_fetch_array($result);

    $session = $row['session_id'];

    function fetchMessages()
    {
$get = ("SELECT * FROM chatRoom WHERE session_id = '$session' ORDER BY chat.timestamp ASC");
$hold = mysql_query($get, $con);
$show = mysql_fetch_array($hold);

return $show;
    }

    if(isset($_GET['submitmsg']))
    {
$message = mysql_real_escape_string($_GET['usermsg']);
$throw = "INSERT INTO chatRoom(session_id, source, message, timestamp) VALUES('".$_GET['id']."', '".$_GET['source']."', '$message', UNIX_TIMESTAMP())";

if (!mysql_query($throw,$con))
{
    die('Error: ' . mysql_error());
}else
{

}
    }
    ?>
<head>
    <title></title>
    <link type="text/css" rel="stylesheet" href="css/chatStyle.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
    <div id="wrapper">
        <div id="menu">
            <p class="welcome"><b>Welcome, <?php echo $row['user_name']; ?></b></p>
            <p class="logout"><a href="nameSub.php?logout=true&name=<?php echo $row['user_name']; ?>&id=<?php echo $row['session_id']; ?>">Exit Chat</a></p>
            <div style="clear:both"></div>
        </div>
        <div id="chatbox">
        </div>
        <form name = "message" action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "get">
            <input type = "hidden" name = "name" value = "<?php echo $_GET['name']; ?>" />
            <input name = "id" type = "hidden" value = "<?php echo $row['session_id']; ?>" />
            <input name = "source" type = "hidden" value = "<?php echo $row['user_name']; ?>" />
            <input name = "usermsg" type = "text" id = "usermsg" size = "63" />  
            <input name = "submitmsg" type = "submit"  id = "submitmsg" value = "Send" />  
        </form> 
    </div>
    <script type="text/javascript">
        $(document).ready(function()
        {
            fetchMessages = function()
            {
                $.ajax
                ({
                    url:'functions.php',
                    type:'post',
                    data:{method:'fetch'},
                    success:function(data)
                    {
                        $('#chatbox').html(data);
                    }
                });
            }

            setInterval(fetchMessages, 5000);
            fetchMessages();
        });
    </script>
</body>

我希望这有帮助

4

1 回答 1

1

functions.php将所需的功能移动到两个文件都包含的外部文件(例如)。

编辑:详细说明:

PHP文件1:

 include('functions.php');
 $myvar = $_GET['myvar']; // jQuery GET variable
 $othervar = $_POST['myvar'];

 commonFunction($myvar,$othervar);

PHP文件2:

include('functions.php');
 $myvar = $_GET['myvar']; // jQuery GET variable
 $othervar = $_POST['myvar'];

 commonFunction($myvar,$othervar);

函数.php:

function commonFunction($var1, $var2) { 
  // Do common proccessing here
}
于 2013-02-28T19:04:39.240 回答