我正在尝试通过ajax将表单中的数据发送到'mess'数据库表中,但我的控制台上没有错误,数据没有插入表中。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/style.css" type="text/css" />
<script src="jquery-3.1.1.js"></script>
<script src="script.js"></script>
// this is the form
<form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST">
<fieldset style="width:40%; margin:0 auto;">
<label for="message" id="message" >Message</label><textarea cols="60" rows="8" id="message" name="message"
required></textarea>
<input type="hidden" id="" name="id" value="<?= $id ?>" />
<input type="hidden" id="topic_id" name="topic_id" value="<?= $topic_id ?>" />
<input type="hidden" id="user_one" name="user_one" value="<?= $user_one ?>" />
<input type="hidden" id="user_two" name="user_two" value="<?= $user_two ?>" />
<input type="submit" name="submitted" id="send" value="Send" />
<span id="error"></span>
</fieldset>
</form>
//这是我的script.js
$(document).ready(function(){
/*post message via ajax*/
$("#send").on("click", function(){
var message = $.trim($("#message").val()),
topic_id = $.trim($("#topic_id").val()),
user_one = $.trim($("#user_one").val()),
user_two = $.trim($("#user_two").val()),
error = $("#error");
if((message != "") && (topic_id != "") && (user_one != "") && (user_two != "")){
error.text("Sending...");
$.post("handlemessageSend.php",{message:message,topic_id:topic_id,user_one:user_one,user_two:user_two}, function(data){
error.text(data);
//clear the message box
$("#message").val("");
});
}
});
//这是我的handlemessageSend.php
require_once("templates/connect.inc.php");
//post message
if(isset($_POST['message'])){
$message = mysqli_real_escape_string($con, $_POST['message']);
$topic_id = mysqli_real_escape_string($con, $_POST['topic_id']);
$user_one = mysqli_real_escape_string($con, $_POST['user_one']);
$user_two = mysqli_real_escape_string($con, $_POST['user_two']);
//insert into `mess table`
$q = mysqli_query($conn, "INSERT INTO mess(sender, reciever, message, topic_id) VALUES ('$user_one','$user_two','$message', '$topic_id')");
if($q){
echo "Posted";
}else{
echo "Error";
}
}