我正在为客户编写一个实时聊天程序,除了从服务器检索数据外,我还能正常工作。我几乎被舔了但是现在我在我的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
}
}
}
}
&GT;
最后是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);
}
});
}
答案 0 :(得分: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。