2016-11-27 36 views
1

,這做的是,當用戶點擊該按鈕時,它將執行的Ajax代碼,然後獲取輸入的值,並將其發送到PHP文件中的函數然後將其發送回Ajax代碼以顯示來自MySQL表的消息。Ajax代碼無法正常工作,即使它是(我認爲)正確

我試着改變我的代碼,改變divID,改變語法,清除代碼塊,但似乎沒有工作。

AJAX

<script> 
$(document).ready(function() { 
    $("#snd").click(function() { 
     var msgg = $('input[name=message]').val(); 
     $.ajax({ 
      type: "POST", 
      url: 'automatedchat_func.php', 
      data: {newmsg: msgg}, 
      success: function(data) { 
       $("#conversation").html(data); 
      } 
     }); 
    }); 
}); 
</script> 

HTML 修訂

<div class="convo"> 
       <div class="convo_field" id="conversation"> 



       </div> 
       <div class="obj"> 
        <div class="txtbox"> 
        <form method="POST"> 
         <input type="input" id="msg" name="message" placeholder="Type exact or related word(s) of your question"/> 
        </form> 
        </div> 
        <div class="but_send"><button id="snd" name="send">SEND</button></div> 
       </div> 
       </div> 

PHP 修訂

<?php 
include 'database/connect.php'; 

session_start(); 


$sql = "SELECT * FROM ai WHERE keywords LIKE '%$_POST[message]%' OR '$_POST[message]%_' OR '$_POST[message]_'"; 
    $result = $conn->query($sql); 
    if ($row = $result->fetch_assoc()) { 
     echo "Hi ". $_SESSION['name'] .".<br> " . $row['message']; 
    } 

?> 
+0

您收到了什麼錯誤?你有沒有從瀏覽器檢查控制檯?並檢查它給出了什麼錯誤 –

+0

那麼你的JS和HTML看起來像它會工作,你確認PHP腳本實際上返回任何東西? MySQL是否失敗 - 是一個實際返回的行..? – Stuart

+0

另外(我希望你已經知道),你的SQL是廣泛存在的濫用(期待幾個鏈接到[鮑比表](https://xkcd.com/327/) – Stuart

回答

0

變化與建議(在評論): -

<div class="convo"> 
    <div class="convo_field" id="conversation"> 
    </div> 
    <div class="obj"> 
     <div class="txtbox"> 
      <input type="input" id="msg" name="message" placeholder="Type exact or related word(s) of your question"/> 
     </div><!-- form not requird --> 
     <div class="but_send"><button id="snd" name="send">SEND</button></div> 
    </div> 
</div> 

<!-- Add jquery library so that jquery code wiil work --> 

<script> 
$(document).ready(function() { 
    $("#snd").click(function() { 
     var msgg = $('#msg').val(); // id is given so use that, its more easy 
     $.ajax({ 
      type: "POST", 
      url: 'automatedchat_func.php', 
      data: {newmsg: msgg}, 
      success: function(data) { 
       $("#conversation").html(data); 
      } 
     }); 
    }); 
}); 
</script> 

automatedchat_func.php(必須是在你上面的html文件存在相同的工作目錄)

<?php 
error_reporting(E_ALL);// check all type of error 
ini_set('display_errors',1);// display all errors 
include 'database/connect.php'; 
session_start(); 
$final_result='';//a variable 
if(isset($_POST['newmsg']) && !empty($_POST['newmsg'])){// its newmsg not message 
    $message = $_POST['newmsg']; 
    $sql = "SELECT * FROM ai WHERE keywords LIKE '%$message%' OR '$message%' OR '$message'"; // check the change ere 
    $result = $conn->query($sql); 
    while($row = $result->fetch_assoc()) { // while needed 
     $final_result .="Hi ". $_SESSION['name'] .".<br> " . $row['message']."<br>"; 
    } 
}else{ 
    $final_result .="Hi please fill the input box first"; 
} 
echo $final_result; // send final result as response to ajax 
?> 

注意: - 您的查詢仍然容易受到SQL注入的影響。因此請閱讀prepared statements並使用它們

+0

謝謝曼!有用! 是的,我知道我的查詢是不安全的,當我完成這篇論文的網站我工作:) – user5567987