I am using a standard example of a chatbox (http://www.phpclasses.org/blog/package/3213/post/1-Tutorial-on-Creating-an-AJAX-based-Chat-system-in-PHP.html) with jQuery 1.7. Everytime I submit I get the message
我正在使用聊天框的标准示例(http://www.phpclasses.org/blog/package/3213/post/1-Tutorial-on-Creating-an-AJAX-based-Chat-system-in-PHP。 html)与jQuery 1.7。我每次提交都会收到消息
Use of getPreventDefault() is deprecated. Use defaultPrevented instead.
不推荐使用getPreventDefault()。请改用defaultPrevented。
The input message is still send to the database and processed correctly, however the "append" to the original source and thus the printing on screen does not work. How can I make this work and prevent the message? My main.js is here:
输入消息仍然发送到数据库并正确处理,但是“附加”到原始源,因此屏幕上的打印不起作用。如何使这项工作并阻止消息?我的main.js在这里:
var lastTimeID = 0;
var lastTimeID = 0;
$(document).ready(function() {
$('#btnSend').click( function() {
sendChatText();
$('#chatInput').val("");
});
startChat();
});
function startChat(){
setInterval( function() { getChatText(); }, 1000);
}
function getChatText() {
$.ajax({
type: "GET",
url: "/taboo/game/refresh.php?lastTimeID=" + lastTimeID
}).done( function( data )
{
var jsonData = JSON.parse(data);
var jsonLength = jsonData.results.length;
var html = "";
for (var i = 0; i < jsonLength; i++) {
var result = jsonData.results[i];
html += '<div ' + result.role + '">(' + result.mes_time + ') <b>' + result.username +'</b>: ' + result.message + '</div>';
lastTimeID = result.messageID;
}
$("#reply").append(html);
});
}
function sendChatText(){
var chatInput = $('#chatInput').val();
if(chatInput != ""){
$.ajax({
type: "GET",
url: "/taboo/game/submit.php?chattext=" + encodeURIComponent( chatInput )
});
}
}
Here the edited version after the first comments: var lastTimeID = 0;
这里是第一条评论后的编辑版本:var lastTimeID = 0;
$(document).ready(function() {
$('#btnSend').click( function() {
sendChatText();
$('#chatInput').val("");
});
startChat();
});
function startChat(){
setInterval( function() { getChatText(); }, 1000);
}
function getChatText() {
$.ajax({
type: "GET",
url: "/taboo/game/refresh.php?lastTimeID=" + lastTimeID,
success: function( data ){
var jsonData = JSON.parse(data);
var jsonLength = jsonData.results.length;
var html = "";
for (var i = 0; i < jsonLength; i++) {
var result = jsonData.results[i];
html += '<div ' + result.role + '">(' + result.mes_time + ') <b>' + result.username +'</b>: ' + result.message + '</div>';
lastTimeID = result.messageID;
}
$("#reply").append("<p>Hello</p>");
console.log(html);
console.log("H");
}
});
}
function sendChatText(){
var chatInput = $('#chatInput').val();
if(chatInput != ""){
$.ajax({
type: "GET",
url: "/taboo/game/submit.php?chattext=" + encodeURIComponent( chatInput )
});
}
}
This is where the input comes from:
这是输入来自的地方:
<div class="reply" id="response"></div>
<div id="ajaxForm">
<input type="text" id="chatInput" /><input type="button" value="Send" id="btnSend" />
</div>
This is the DB connection:
这是数据库连接:
class chatClass
{
public static function getRestChatLines($messageID)
{
$arr = array();
$jsonData = '{"results":[';
include "/Applications/XAMPP/xamppfiles/htdocs/taboo/game/connectToDB.php";
$_db->query("SET NAMES utf8");
$statement = $_db->prepare("SELECT * FROM messages WHERE message_id > ? and mes_time >= DATE_SUB(NOW(), INTERVAL 1 HOUR)");
$statement->bindParam('i', $messageID);
$statement->execute(array($messageID));
while ($statement->fetch()) {
$line->message_id = $messageID;
$line->game_id =$gameID;
$line->user_name = $username;
$line->role = $role;
$line->message = $message;
$line->ordering = $ordering;
$line->mes_time = date('H:i:s', strtotime($mes_time));
$arr[] = json_encode($line);
}
$link = null;
$jsonData .= implode(",", $arr);
$jsonData .= ']}';
return $jsonData;
}
public static function setChatLines($message, $username, $role, $gameID, $ordering) {
include "/Applications/XAMPP/xamppfiles/htdocs/taboo/game/connectToDB.php";
$statement = $_db->prepare( "INSERT INTO messages(message, user_name, role, game_id, $ordering) VALUES(?, ?, ?, ?,)");
$statement->execute(array($message, $username, $role, $gameID, $ordering));
$link = null;
}
}
?>
And the submit in the middle that passes the data from the main.js (first code) to the DB php (just before this).
中间的提交将数据从main.js(第一个代码)传递到DB php(就在此之前)。
<?php
session_start();
require_once( "/Applications/XAMPP/xamppfiles/htdocs/taboo/game/connectToDB.php" );
require_once( "/Applications/XAMPP/xamppfiles/htdocs/taboo/game/chatClass.php" );
$message = htmlspecialchars( $_GET['chattext'] );
echo $message;
$username=$_SESSION['username'];
$ordering = $_SESSION['ordering'];
++$ordering;
$_SESSION['ordering'] = $ordering;
chatClass::setChatLines( $message, $username, $_SESSION['role'], $_SESSION['gameID'], $ordering);
?>
This is the refresh that updates and checks for updates periodically.
这是定期更新和检查更新的刷新。
<?php
require_once( "/Applications/XAMPP/xamppfiles/htdocs/taboo/game/connectToDB.php" );
require_once( "/Applications/XAMPP/xamppfiles/htdocs/taboo/game/chatClass.php" );
$message_id = intval( $_GET[ 'lastTimeID' ] );
$jsonData = chatClass::getRestChatLines( $message_id );
print $jsonData;
?>
Those are all elements related to the chatbox that doesn't work. Thanks in advance for your help - I have been trying everything I could find in this forum and others and nothing gets my text returned to the bowerser
这些都是与聊天框无关的元素。在此先感谢您的帮助 - 我一直在尝试在这个论坛和其他人中找到的所有内容,没有任何内容可以让我的文字返回给揽客
1 个解决方案
#1
0
$("#reply").append("<p>Hello</p>");
this will select element with id="reply" but in your html there are no id="reply"
这将选择id =“reply”的元素,但在你的html中没有id =“reply”
you can append it by
你可以追加它
$('#response').append("<p>Hello</p>")
this will select tag with id="response"
and append the hello
这将选择id =“response”的标签并追加你好
#1
0
$("#reply").append("<p>Hello</p>");
this will select element with id="reply" but in your html there are no id="reply"
这将选择id =“reply”的元素,但在你的html中没有id =“reply”
you can append it by
你可以追加它
$('#response').append("<p>Hello</p>")
this will select tag with id="response"
and append the hello
这将选择id =“response”的标签并追加你好