php& JS聊天,在聊天窗口中执行html

时间:2014-01-23 18:55:15

标签: javascript php jquery html mysql

我已经尝试过在聊天窗口中获取html输出但是它显示的是html代码

这是聊天系统中涉及的2个文件

chat.js

 `function chatHeartbeat(){

var itemsfound = 0;

if (windowFocus == false) {

    var blinkNumber = 0;
    var titleChanged = 0;
    for (x in newMessagesWin) {
        if (newMessagesWin[x] == true) {
            ++blinkNumber;
            if (blinkNumber >= blinkOrder) {
                document.title = x+' says...';
                titleChanged = 1;
                break;  
            }
        }
    }

    if (titleChanged == 0) {
        document.title = originalTitle;
        blinkOrder = 0;
    } else {
        ++blinkOrder;
    }

} else {
    for (x in newMessagesWin) {
        newMessagesWin[x] = false;
    }
}

for (x in newMessages) {
    if (newMessages[x] == true) {
        if (chatboxFocus[x] == false) {
            //FIXME: add toggle all or none policy, otherwise it looks funny
            $('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink');
        }
    }
}

$.ajax({
  url: "chat.php?action=chatheartbeat",
  cache: false,
  dataType: "json",
  success: function(data) {

    $.each(data.items, function(i,item){
        if (item)   { // fix strange ie bug

            chatboxtitle = item.f;

            if ($("#chatbox_"+chatboxtitle).length <= 0) {
                createChatBox(chatboxtitle);
            }
            if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
                $("#chatbox_"+chatboxtitle).css('display','block');
                restructureChatBoxes();
            }

            if (item.s == 1) {
                item.f = username;
            }

            if (item.s == 2) {
                $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
            } else {
                newMessages[chatboxtitle] = true;
                newMessagesWin[chatboxtitle] = true;
                $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
            }

            $("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
            itemsfound += 1;
        }
    });

    chatHeartbeatCount++;

    if (itemsfound > 0) {
        chatHeartbeatTime = minChatHeartbeat;
        chatHeartbeatCount = 1;
    } else if (chatHeartbeatCount >= 10) {
        chatHeartbeatTime *= 2;
        chatHeartbeatCount = 1;
        if (chatHeartbeatTime > maxChatHeartbeat) {
            chatHeartbeatTime = maxChatHeartbeat;
        }
    }

    setTimeout('chatHeartbeat();',chatHeartbeatTime);
}});
}` 

这里item.m包含聊天消息,我尝试通过添加.html()来获取html输出,但它无法正常工作

这里是与php文件相同的功能

     function chatHeartbeat() {

$sql = "select * from chat where (chat.to =     '".mysql_real_escape_string($_SESSION['username'])."' AND recd = 0) order by id ASC";
$query = mysql_query($sql);
$items = '';

$chatBoxes = array();

while ($chat = mysql_fetch_array($query)) {

    if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
        $items = $_SESSION['chatHistory'][$chat['from']];
    }

    $chat['message'] = sanitize($chat['message']);

    $items .= <<<EOD
                   {
        "s": "0",
        "f": "{$chat['from']}",
        "m": "{$chat['message']}"
   },
EOD;

if (!isset($_SESSION['chatHistory'][$chat['from']])) {
    $_SESSION['chatHistory'][$chat['from']] = '';
}

$_SESSION['chatHistory'][$chat['from']] .= <<<EOD
                       {
        "s": "0",
        "f": "{$chat['from']}",
        "m": "{$chat['message']}"
   },
 EOD;

    unset($_SESSION['tsChatBoxes'][$chat['from']]);
    $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
}

if (!empty($_SESSION['openChatBoxes'])) {
foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
    if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
        $now = time()-strtotime($time);
        $time = date('g:iA M dS', strtotime($time));

        $message = "Sent at $time";
        if ($now > 180) {
            $items .= <<<EOD
 {
 "s": "2",
 "f": "$chatbox",
 "m": "{$message}"
 },
 EOD;

if (!isset($_SESSION['chatHistory'][$chatbox])) {
    $_SESSION['chatHistory'][$chatbox] = '';
}

$_SESSION['chatHistory'][$chatbox] .= <<<EOD
    {
 "s": "2",
 "f": "$chatbox",
 "m": "{$message}"
  },
 EOD;
        $_SESSION['tsChatBoxes'][$chatbox] = 1;
    }
    }
}
 }

$sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['username'])."' and recd = 0";
$query = mysql_query($sql);

if ($items != '') {
    $items = substr($items, 0, -1);
}
  header('Content-type: application/json');
 ?>
 {
         "items": [
             <?php echo $items;?>
         ]
  }

<?php
        exit(0);
}

请建议我在聊天窗口中获取html输出的任何想法,以及用户可以点击聊天窗口中的链接

谢谢

1 个答案:

答案 0 :(得分:0)

可能是因为您清理了消息,将HTML转换为字符..

$chat['message'] = sanitize($chat['message']);