在创建jquery+PHP+MySQL聊天室时遇到问题


having problems creating jquery + PHP +MySQL chatroom

我已经阅读了几篇关于jquery和JavaScript中使用PHP和mysql后端的图表室的教程。我知道php部分会起作用(或者如果它不起作用,那么我可以比likley更容易地修复它),但问题在于我对jquery的了解极其有限。因此,希望这里有人能深入了解我(可能非常明显)的错误所在,我将不胜感激

此外:我并不是在寻找一个人来向我展示需要做什么,只是朝着正确的方向努力,因为我找不到任何真正有用的东西。

主页:

<?php
include 'connect.php';
session_start();
include 'include/notloggedin.php';
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="windows-1252">
    <title>zyrnith</title>
    <link href="stylesheets/all.css" rel="stylesheet" type="text/css"/>
    <link href="stylesheets/game.css" rel="stylesheet" type="text/css"/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
                function scTop(){
     $("#messages").animate({scrollTop:$("#messages")[0].scrollHeight});
    }
    function load_new_stuff(){
     localStorage['lpid']=$(".messagerow:last").attr("title");
     $("#messages").load("include/messages.php",function(){
      if(localStorage['lpid']!=$(".messagerow:last").attr("title")){
       scTop();
      }
     });
    }
    $(document).ready(function(){
     scTop();
     });
    $('#chatbox').keydown(function() {
    if (event.keyCode == 13) {
      val=$(this).find("#chatbox").val();
      if (val != ""){
       $.post("include/sendchat.php",{message:val},function(){
        load_new_stuff();
       });
      }
      return false;
  })
  });

    $( document ).ready(function()
    {
        setInterval(load_new_stuff(), 1000);
    });
    </script>
</head>
<body>
    <div id="everything">
        <div style="width:200px; float: left;">
            test
        </div>
        <div id="chat">
            <div id="messages">
            <?php
                include 'include/messages.php';
            ?>
            </div>
            <form id="chatform">
                <input type="text" id="chatbox" maxlength="150" name="message" />
            </form>
        </div>
        <div style="float: right; text-align: right; width:200px;">
            <?php echo $_SESSION['username']; ?>
            <br />
            <a style="height: 150px; padding-right: 5px; font-variant: small-caps;" href="logout.php">Logout</a>
        </div>
    </div>
</body>
</html>

sendchat.hp:

<?php
include("connect.php");
if(!isset($_SESSION['userid']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])=='xmlhttprequest'){
 die("<script>window.location.reload()</script>");
}
if(isset($_SESSION['userid']) && isset($_POST['message']))
{
 $message=htmlspecialchars($_POST['message']);
 if($message){
  mysql_query("INSERT INTO chat (user,message) VALUES ('$_SESSION['userid']','$message')") or die(mysql_error());
 }
}
?>

你需要的任何其他信息都可以告诉我,我可以更新。

文件结构:

  • 包括
    • messages.php
    • notloggedin.php
    • sendchat.php
  • changelog.txt
  • connect.php
  • 错误.php
  • 聊天.php
  • index.php
  • login.php
  • 注销.php
  • register.php

未捕获的SyntaxError:意外的令牌)--第33行

如果您可以从正在使用的浏览器中共享Javascript控制台,则可以显示问题所在的一些见解。如果您是新开发人员,请使用以下内容显示控制台:

在chrome和Mac上:cmd+选项(alt)+I将打开控制台。在该窗口上选择最后一个选项Console。

此外,在你错过的代码的开头,不确定你在复制代码时是否错过了。

<?php

在服务器端,您可以检查php相关的错误。

error.log
   function scTop(){
        $("#messages").animate({scrollTop:$("#messages")[0].scrollHeight});
    }
    function load_new_stuff(){
        localStorage['lpid']=$(".messagerow:last").attr("title");
        $("#messages").load("include/messages.php",function(){
            if(localStorage['lpid']!=$(".messagerow:last").attr("title")){
                scTop();
            }
        });
    }
    $(document).ready(function(){
        scTop();
    });
    $('#chatbox').keydown(function() {
        if (event.keyCode == 13) {
            val=$(this).find("#chatbox").val();
            if (val !== ""){
                $.post("include/sendchat.php",{message:val},function(){
                    load_new_stuff();
                });}}});

    $( document ).ready(function()
    {
        setInterval(load_new_stuff(), 1000);
    });