Ajax代码即使(我认为)正确也无法正常工作

时间:2016-11-27 07:42:46

标签: php jquery mysql ajax

这样做的功能是当用户点击按钮时,它将执行Ajax代码,然后获取输入的值并将其发送到PHP文件,然后将其发送回Ajax代码以显示消息来自MySQL表。

我尝试更改代码,更改div ID,更改语法,清除代码块但似乎没有效果。

  

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'];
    }

?>

1 个答案:

答案 0 :(得分: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并使用它们