【问题标题】:Ajax post / insert into database not workingAjax发布/插入数据库不起作用
【发布时间】:2016-04-21 09:43:01
【问题描述】:

我是 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";
    }
}

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

【问题讨论】:

  • 做一件事。将 $mysqli-&gt;query 替换为 die 并复制查询并将其粘贴到 phpMyAdmin 或 Adminer 或其他任何内容中,然后告诉您所看到的。同时显示你得到的查询是什么。
  • 浏览器是否返回了任何内容。 showComment() 做了什么,您没有将 data 传递给该函数,这就是问题所在。签入调试器
  • 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},
  • 但是为了测试它不需要预防是的,是的,我们都知道这意味着什么
  • 在我尝试使用 Ajax 之前,我将所有内容都放在一个文件中,并且工作正常 不是特别相关。如果你改变了它的工作方式,那就是小错误被忽视的时候

标签: php jquery ajax


【解决方案1】:

试试下面的

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
        }
    });
});

php

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

【讨论】:

    【解决方案2】:

    试试这个:

    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"];
        ....
    }
    

    【讨论】:

    • 为什么 OP 应该“试试这个”? 好的答案将始终解释所做的事情以及这样做的原因,不仅适用于 OP,而且适用于可能会发现此问题并正在阅读您的答案的 SO 的未来访问者。
    【解决方案3】:

    您在第二个 AJAX 调用中缺少“includes/ajax.cmets”的文件扩展名,该调用在 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+"&...
    

    【讨论】:

    • @gravgrif 我添加了文件扩展,但它无论如何都不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2017-10-07
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多