追加无法在php/ajax中使用 [英] Append not working in php/ajax
问题描述
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
不赞成使用getPreventDefault().改用defaultPrevented.
Use of getPreventDefault() is deprecated. Use defaultPrevented instead.
输入消息仍将发送到数据库并已正确处理,但是追加"到原始来源,因此无法在屏幕上打印.我该如何进行这项工作并阻止该消息?我的main.js在这里:
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:
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 )
});
}
}
在第一个评论之后是编辑后的版本: var lastTimeID = 0;
Here the edited version after the first comments: 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 )
});
}
}
这是输入的来源:
<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;
}
}
?>
然后在中间提交,将数据从main.js(第一个代码)传递到DB php(就在此之前).
And the submit in the middle that passes the data from the main.js (first code) to the DB php (just before this).
<?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;
?>
与聊天框相关的所有元素均无效. 在此先感谢您的帮助-我一直在尝试在该论坛和其他论坛中可以找到的所有内容,但没有任何内容可以将我的文字退还给Bowerser
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
推荐答案
$("#reply").append("<p>Hello</p>");
这将选择id ="reply"的元素
但是在你的HTML中
没有id="reply"
this will select element with id="reply"
but in your html
there are no id="reply"
您可以通过以下方式附加它
you can append it by
$('#response').append("<p>Hello</p>")
这将选择带有id="response"
的标签并追加问候
this will select tag with id="response"
and append the hello
这篇关于追加无法在php/ajax中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!