我好像解决不了这个问题。经过几天的重写,至今仍未取得任何进展,我无法指望。
以下是Javascript (与html页面相同)
摘要:用户在输入框中键入文本。这将被发送到被处理,然后被发回并显示在屏幕上的框ID作为DisplayText在html页面上。
<script type="text/javascript">
function SendText() {
if (document.getElementById("Text").innerHTML == "") {
return;
} else
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("DisplayText").innerHTML = xmlhttp.responseText;
}
}
Text = document.getElementById("Text").value;
xmlhttp.open("GET", "php/Test.php?Chat=" + Text, true);
xmlhttp.send();
}
}
</script>下面是HTML (与脚本相同的页面)
<div>
<p>
<span id="DisplayText">
TEXT GOES HERE
</span>
</p>
</div>
<form action="" onsubmit="SendText();">
<input type="" name="" id="Text" />
<input type="submit" value="Send" name="Send" />
</form>PHP代码在这里
<?php
session_start();
include ("Connect.php");
$Connection = mysqli_connect("localhost", "root", "", "chatsystem");
$Create = "CREATE TABLE " . $_SESSION["Username"] . "Chat(Username VARCHAR(255), Chat VARCHAR(255))";
////////////////////////////////////////////////////////////////////////////////
$DatabaseExist = $Connection->query("SELECT 1 FROM " . $_SESSION["Username"] . "Chat");
if ($DatabaseExist !== false) {
echo "Database exists";
doSomething();
} else {
echo "Database does not exist";
mysqli_query($Connection, $Create);
doSomething();
}
////////////////////////////////////////////////////////////////////////////////
function doSomething() {
// Get the sent chat
$Input = $_REQUEST["Chat"];
// Insert into the database the new chat sent
mysqli_query($Connection, "INSERT INTO " . $_SESSION["Username"] . "chat (`Username`, `Chat`) VALUES ('$_SESSION[Username], '$Input')");
// Select everything from the database
$Result = $Connection->query("SELECT * FROM " . $_SESSION["Username"] . "Chat");
// Display everything from the database in an orderly fashion
// --
// For the length of the database
// Append to a variable the next table row
// --
while ($Row = $Result->fetch_array()) {
// Make Variable accessable
global $Input;
// Append username to the return value
$Input = $Input . $Row["Username"] . " : ";
// Append chat to the return value
$Input = $Input . $Row["Chat"] . "<br />";
}
}
// Will return the value
echo $Input;
?>我和数据库的连接很好。我把它用在了其他有用的页面上。所以假设这不是问题所在。:P
任何帮助或洞察力,谁知道或能想到的事情是错误的,我将非常感激。我对AJAX很陌生。
发布于 2014-01-13 10:45:46
1 :使用输入的value属性代替innerHTML
例如:使用
if (document.getElementById("Text").value == "")而不是
if (document.getElementById("Text").innerHTML == "")2 :在表单的onsubmit事件中使用return false;;防止当前页面在使用ajax时刷新。否则页面将被刷新,它将不会显示php页面的输出,
例如:使用
onsubmit="SendText(); return false;"而不是仅仅
onsubmit="SendText();"发布于 2014-01-13 10:09:09
你做了一个错误的测试
if (document.getElementById("Text").innerHTML == "")应该使用相同的方式获取用于在AJAX中发送的文本。
if (document.getElementById("Text").value == "")所以检查它的value属性,而不是它的innerHTML,因为input元素没有html内容。
但是要小心,因为您的代码对SQL注入攻击非常开放。
发布于 2014-01-13 10:10:45
尝试用于聊天应用程序的AJAX长轮询技术。这是一个例子
http://portal.bluejack.binus.ac.id/tutorials/webchatapplicationusinglong-pollingtechnologywithphpandajax
https://stackoverflow.com/questions/21088484
复制相似问题