0

我正在为客户编写一个实时聊天程序,除了从服务器检索数据外,我一切正常。我几乎舔了它,但现在我在我的 jquery 函数中收到此错误,告诉未捕获的语法错误:意外标识符,我尝试了此代码的几种变体,但它一直给我这个问题。这是代码

    <!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';
    include 'function.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 = json_encode($row['session_id']);
    /*
    function fetchMessages()
    {
        $get = ("SELECT * FROM chatRoom WHERE session_id = '$session'");
        $hold = mysql_query($get, $con);

        if($hold)
        {
            return mysql_fetch_array($hold);
        }
    }
    */
    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
        {
            fetchMessages();
        }
    }
    ?>
<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()
            {
                var sess = <?php echo $session; ?>;
                $.ajax
                ({
                    url:'functions.php',
                    type:'post',
                    data:{method:'fetch, sess'},
                    success:function(data)
                    {
                        $('#chatbox').html(data);
                    }
                });
            }

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

当我尝试创建变量 sess 以传递给被调用的脚本时,就会出现问题,这样我就可以将它传递给必要的函数来检索数据。

好的,这是更新的 jquery

    fetchMessages = function()
    {
        var sess = <?php echo $session; ?>;
        $.ajax
        ({
            url:'functions.php',
            type:'post',
            data:{method:'fetch'},
            success:function(data)
            {
                $('#chatbox').html(data);
            }
        });
    }

和functions.php

    <?php
    include 'core/conection.php';
    include 'function.php';

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

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

    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
        }
    }
}
    }

?>

最后是 fetchMessages()

    <?php
    include 'core/conection.php';

    function fetchMessages($session)
    {
        $session = $_GET['$session'];
        $get = ("SELECT * FROM chatRoom WHERE session_id = '$session'");
        $hold = mysql_query($get, $con);

        if($hold)
        {
            return mysql_fetch_array($hold);
        }
    }
    ?>

我知道我在这里的某个地方有错误,因为我仍然没有显示

最新更新 jquery 代码,这将 session_id 显示为整数但是我仍然得到意外的标识符

    fetchMessages = function()
{
    $.ajax
    ({
        url:'functions.php',
        type:'post',
        data:{method:'fetch'
              session:'<?php echo $row['session_id']; ?>'},
        success:function(data)
        {
            $('#chatbox').html(data);
        }
    });
}
4

1 回答 1

2

我将此添加为一个答案,因为我相信这是您的问题之一。如果 sess 是 JSON 编码的字符串,并且您想将其传递给您需要的函数 php:

data: {method: 'fetch', session: sess}

或者至少:

data: {method: 'fetch, '+sess}

查看您的其他代码fetchMessages()

$session = $_GET['$session'];

这不是必需的,因为您将 $session 传递给函数。它不会在 $_GET 范围内。

也来自functions.php

$session = trim($_POST['session'];

在您更新的 javascript 中,您甚至没有在会话中发布任何内容,而只是在方法中发布。您需要添加 session: 'something' 以将其发送到 function.php。

于 2013-02-28T21:01:54.263 回答