user5567987 user5567987 - 5 days ago 5
PHP Question

Ajax code not working even if it is (i think) correct

The function that this do is when the user clicked the button, it will execute the ajax codes and then get the value of the input and send it to the PHP file and then send it back to the ajax code to display the message from the mysql table.

I tried changing my codes, changing div ids, changing syntax, clearing block of codes but none seems to work.


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 UPDATED


<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 UPDATED


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

?>

Answer

Changes with suggestion(in comment):-

<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(must be in the same working directory where your above html file exist)

<?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
?>

Note:- your query is still vulnerable to SQL Injection. So read for prepared statements and use them

Comments