Ajax 发布/插入到数据库中不起作用


Ajax post / insert into database not working

我是 ajax 的新手,在提交表单和通过 ajax 发送数据时遇到问题。从数据库中获取数据不是问题。但是插入不起作用,我没有发现错误。

我的表单文件

<script type="text/javascript">
    $(document).ready(function(){
        function showComment(){
            $.ajax({
                type:"post",
                url:"./includes/ajax.comments.php",
                data:"post_id=<?PHP echo $post->post_id ?>&action=show",
                success:function(data){
                    $("#comments").html(data);
                }
            });
        }
        showComment();
        $("#fastReply").click(function(e){
            e.preventDefault();
            var comment_text = $("#txtArea").val();
            var comment_user_id = <?PHP echo $current_user->user_id; ?>;
            var comment_post_id = <?PHP echo $post->post_id; ?>;
            $.ajax({
                type:"post",
                url:"./includes/ajax.comments.php",
                data: "comment_title=null&comment_text="+comment_text+"&comment_user_id="+comment_user_id+"&comment_post_id="+comment_post_id+"comment_status=1&action=add",
                success: function(data){
                    showComment();
                }
            });
        });
    });
</script>
<form action="post">
    <textarea id="txtArea"></textarea>
    <button id="fastReply" type="submit">Post</button>
</form>

我的 php 文件看起来像这样

if($action == "add"){
    header("Content-Type: application/json", true);
    $comment_title      = $_POST["comment_title"];
    $comment_text       = $_POST["comment_text"];
    $comment_user_id    = $_POST["comment_user_id"];
    $comment_post_id    = $_POST["comment_post_id"];
    $comment_status     = $_POST["comment_status"];
    $query = $mysqli->query("INSERT INTO comments(comment_title, comment_text, comment_user_id, comment_post_id, comment_status) 
                                VALUES('".$comment_title."', '".$comment_text."', ".$comment_user_id.", ".$comment_post_id.", ".$comment_status.")");
    if($query){
        echo "Your comment has been sent";
    } else {
        echo "Error in sending your comment";
    }
}

也没有找到其他对我有帮助的问题

尝试以下操作

function showComment(id){
    $.ajax({
        type:"post",
        url:"./includes/ajax.comments.php",
        data:{post_id:id,action:'show'},//convert to object
        success:function(data){
            $("#comments").html(data);
        }
    });
}
$("#fastReply").click(function(e){
    e.preventDefault();
    var comment_text = $("#txtArea").val();
    var comment_user_id = '<?PHP echo $current_user->user_id; ?>';//convert to string
    var comment_post_id = '<?PHP echo $post->post_id; ?>';//convert to string
    $.ajax({
        type:"post",
        url:"./includes/ajax.comments",
        data: {
            comment_title: null,
            comment_text: comment_text,
            comment_user_id: comment_user_id,
            comment_post_id: comment_post_id,
            comment_status: 1,
            action: 'add'
        },//convert to object
        success: function(data){
            showComment(id);//pass the id
        }
    });
});

菲律宾比索

$action = $_POST['action'];//get the action value

试试这个:

if($_POST["action"] == "add")
{
    header("Content-``Type: application/json", true);
    $comment_title      = $_POST["comment_title"];
    $comment_text       = $_POST["comment_text"];
    $comment_user_id    = $_POST["comment_user_id"];
    $comment_post_id    = $_POST["comment_post_id"];
    ....
}

您在第二次 AJAX 调用中缺少"include/ajax.comments"的文件扩展名,该调用在 fastReply 单击时触发,因此不会运行

$("#fastReply").click(function(e){...//this section
//other code
    $.ajax({
        type:"post",
        url:"./includes/ajax.comments.php",//this is where it is missing & i added it
        data: "comment_title=null&comment_text="+comment_text+"&...