(对拼写错误的单词表示歉意) 大家好,大家好 希望你玩得开心. 我是第一次使用AJAX,并试图通过AJAX将一些元素(发送和接收的消息)添加到html中,但我唯一得到的是一个未定义的html文本.
messageInsert和第一次访问正常,但从get_msg我得到一个sql语法错误:
致命错误:未捕获MySQLSQL_EXCEPTION:您的SQL语法有错误;请查看与您的MariaDB服务器版本对应的手册,了解正确的语法,以便在C:\xampp\htdocs\project\myProjects\Chatbot\php\get_msg.php:9堆栈跟踪的第1行使用NEAR‘AND INPING_ID=)OR(OUTENT_ID=AND INFING_ID=)ORDER BY MSG_ID DESC’:#0 C:\xampp\htdocs\project\myProjects\Chatbot\php\get_msg.php(9):MYSQLI->;查询(‘SELECT*FROM m...’)#1{Main}在第9行的C:\xampp\htdocs\project\myProjects\Chatbot\php\get_msg.php中抛出
MessageInsert.php:
<?php
session_start();
require_once 'connection.php';
if (isset($_SESSION['userID'])) {
$outgoing_id = mysqli_real_escape_string(connection() , $_POST['outgoing_id']);
$incoming_id = mysqli_real_escape_string(connection() , $_POST['incoming_id']);
$message = mysqli_real_escape_string(connection() , $_POST['message']);
if (!empty($message)) {
$query = "INSERT INTO messages (outgoing_id, incoming_id, msg) VALUES ($outgoing_id , $incoming_id , '$message')";
$result =connection()->query($query);
}
}
Get_msg.php:
<?php
session_start();
require_once 'connection.php';
if (isset($_SESSION['userID'])) {
$outgoing_id = mysqli_real_escape_string(connection() , $_POST['outgoing_id']);
$incoming_id = mysqli_real_escape_string(connection() , $_POST['incoming_id']);
$output = "";
$query = "SELECT * FROM messages WHERE (outgoing_id = {$outgoing_id} AND incoming_id = {$incoming_id}) OR (outgoing_id = {$incoming_id} AND incoming_id = {$outgoing_id}) ORDER BY msg_id DESC";
$result = connection()->query($query);
while ($row = mysqli_fetch_assoc($result)) {
if ($outgoing_id === $row['outgoing_id']) {
$output ='<div class="message-send my-4">
<p>'.$row['msg'].'</p>
</div>';
} else {
$output = '<div class="message-receive">
<img class="message-receive-pic mb-3 me-2" src="style/ProfilePic/'.$_SESSION['userPic'].'" alt="">
<div class="message-receive-text-box my-4">
<p> '. $row['msg'] .'</p>
</div>
</div>';
}
echo $output;
}
}
我的名字:
let form = document.querySelector(".mySend"),
sendInput = form.querySelector(".message"),
sendBtn = form.querySelector(".sendBtn"),
chatBox = document.querySelector(".chat-box");
form.onsubmit = (e)=> {
e.preventDefault();
}
sendBtn.onclick = ()=> {
let xhr = new XMLHttpRequest();
xhr.open("POST" , "http://localhost/project/myProjects/Chatbot/php/messageInsert.php" , true);
xhr.onreadystatechange = ()=> {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200){
sendInput.value = "";
}
}
}
let formData = new FormData(form);
xhr.send(formData);
}
setInterval(()=> {
let xhr = new XMLHttpRequest();
let data = "";
xhr.open("POST" , "php/get_msg.php" , true);
xhr.onload = ()=> {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
data = this.response;
chatBox.innerHTML = data;
}
}
}
let formData = new FormData(form);
xhr.send(formData);
}, 500);
我的html:
<div class="chat-box">
</div>
<div class="type-box">
<form class="mySend" method="post" autocomplete="off">
<input type="hidden" value="<?php echo $userProfile['id'] ?>" name="incoming_id">
<input type="hidden" value="<?php echo $_SESSION["profileID"] ?>" name="outgoing_id">
<input class="message form-control" type="text" placeholder="Type your message here..." name="message">
<button type="submit" class=" sendBtn btn btn-dark" name="sendBtn"><i class="fab fa-telegram-plane"></i></button>
</form>
</div>
我对表名和列名很确定.
非常感谢您的帮助:)
我已经try 了AJAX和php文件中的所有方法,都没有奏效.