2014-01-13 80 views
0

我似乎無法工作這一個。過了幾天,重寫它的次數仍然沒有超過我的指數。AJAX聊天系統不工作

這裏是JavaScript(在同一個頁面的html)

摘要:用戶類型文本輸入框。這將被髮送出去處理,然後發回並顯示在HTML頁面上的框ID'd中作爲DisplayText的屏幕上。

<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新手。

+0

使用JavaScript框架(JQuery的建議)用於這一目的,只要阿賈克斯的擔憂。對於長輪詢技術,請參閱本教程http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery。 –

回答

0

1:使用輸入的value屬性,而不是innerHTML

如。使用

if (document.getElementById("Text").value == "") 

,而不是

if (document.getElementById("Text").innerHTML == "") 

第二:使用return false;在窗體的onsubmit事件;到防止當前頁面刷新,因爲您正在使用ajax。否則,頁面將刷新,並且不會顯示php頁面的輸出,例如

例如。使用

onsubmit="SendText(); return false;" 

,而不是僅僅

onsubmit="SendText();" 
+0

由於你的答案給了我最多的意見,所以我將它標記爲答案。由於這兩個答案,它爲我減少了一點時間,試圖弄清楚爲什麼它會令人耳目一新,這將是一場艱苦的鬥爭。所以謝謝。 – Hamedar

+0

很高興它爲你工作.. –

+1

@ GabyakaG.Petrioli嗯,但SQL注入是服務器端的擔憂;它不是問題的一部分。 –

1

你做了錯誤的測試與

if (document.getElementById("Text").innerHTML == "") 

它應該是你用它來獲取文本在AJAX

if (document.getElementById("Text").value == "") 

發送相同的方式,以便檢查其value屬性,而不是它的innerHTML as input elements do not have html content ..


請小心,因爲您的代碼是全開到SQL injection attacks

+0

我試圖讓它工作,然後我要去除除字母和數字之外的所有字符的文本,應該處理幾個SQL注入攻擊?不過謝謝你的這個代碼。它幫助我調試它。 – Hamedar

+0

@Hamedar防止注入的最佳保護措施是使用準備好的語句,只接受用戶輸入的參數而不是字符串連接。 –

+0

如果我將所有字符都去掉,那麼保護是否足以阻止任何和所有注入攻擊?不按字母順序和/或數字?那樣他們就沒有能力使用php和/或sql編碼了? 如果他們不能與數據庫通信,他們不會造成任何問題。 但是我可以看到你的意思,一個準備好的聲明將簡單地刪除任何和所有能夠做任何事情的能力。 – Hamedar