Ajax没有更新数据

Ajax is not updating data

本文关键字:数据 更新 Ajax      更新时间:2023-09-26

我有一个论坛,在这个论坛中,用户只允许编辑和删除他的评论,我定义了一个"编辑"按钮,只需点击鼠标就可以放下一个模态,在这个模态中,用户可以访问他/她以前发送过的数据,我写了一个ajax来针对这些字段,并在用户点击"编辑"键时更新它们,代码完全有意义,但到目前为止,为了更清楚地表明,该功能还没有,用户点击,modal关闭,他/她发布的任何内容都会出现在字段中,modal底部有一个"编辑"按钮,负责更改和更新数据。这是模式代码:

                <button id="btn-btnedit"   class="btn btn-primary " data-toggle="modal" data-target="#myModal<?php echo $list['id']; ?>">
                  Edit <i class="fa fa-pencil-square-o"></i>
                </button>

                <!-- Modal -->
                <div class="modal fade" id="myModal<?php echo $list['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                  <div class="modal-dialog">
                    <div class="modal-content">
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                      </div>
                      <div class="modal-body">
                        <div class="container">
                                    <form style="width: 550px;" action="" method="post" id="signin-form<?php echo $list['id']; ?>" role="form">
                                    <input type="hidden" name="commentID" value="<?php echo $list['id']; ?>">
                                    <div class="from-group">
                                        <label for="title">Title: </label>
                                        <input class="form-control" type="text" name="title" id="txttitle" value="<?php echo $list['title']; ?>" placeholder="Page Title">
                                    </div>
                                    <div class="from-group">
                                        <label for="label">Label: </label>
                                        <input class="form-control" type="text" name="label" id="txtlabel" value="<?php echo $list['label']; ?>" placeholder="Page Label">
                                    </div>
                                    <br>
                                     <div class="from-group">
                                        <label for="body">Body: </label>
                                        <textarea class="form-control editor" name="body" id="txtbody" row="8" placeholder="Page Body"><?php echo $list['body']; ?></textarea>
                                    </div>
                                    <br>
                                    <input type="hidden" name="editted" value="1">
                                    <br>
                                    <br>
                                    <input type="submit" id="btnupdate" value="Edit">
                                </form>
                        </div>
                      </div>

正如您所看到的,我已经将"edited"分配给了我的"name"属性,该属性稍后用于调用数据库中的查询,sql代码如下:

        case 'postupdate';
        if(isset($_GET['editted'])){
                $title = $_GET['title'];
                $label = $_GET['label'];
                $body = $_GET['body'];
                    $action = 'Updated';
                    $q = "UPDATE posts SET title ='".$title."', label = '".$label."', body = '".$body."' WHERE id = ".$_GET['commentID'];
                    $r = mysqli_query($dbc, $q);

                    $message = '<p class="alert alert-success"> Your Post Is Succesfully '.$action.'</p>' ; 

            }

下面是ajax代码片段;

    $('#btnupdate').click(function() {
    var tempTitle = $('#txttitle').val();
    var tempLabel = $('#txtlabel').val();
    var tempBody = $('#txtbody').val();
    var tempUrl = "index.php?page=postupdate"+"&title="+tempTitle+"&label="+tempLabel+"&body="+tempBody+"&commentID=30&editted=1";
    $.get(tempUrl);
});

我认为这段代码没有任何进展,我错过了一些非常简单的东西,任何考虑都将受到高度赞赏:)

这(未经测试的代码)可能与您应该做的类似:

$('#btnupdate').click(function() {
    var tempTitle = $('#txttitle').val();
    var tempLabel = $('#txtlabel').val();
    var tempBody = $('#txtbody').val();
    var tempParams = {"page":"postupdate","title":tempTitle,"label":tempLabel,"body":tempBody,"commentID":30,"editted":1};
    $.post("index.php",tempParams,function(data) {
        alert(data);
    });
});

更新

请尝试ajax,而不是查看加载时是否出现错误

$.ajax( {url:"index.php",data:tempParams,type: "POST"} ).done(function() {
    alert( "success" );
}).fail(function() {
    alert( "error" );
}).always(function() {
    alert( "complete" );
});`

更新

开始测试点击处理程序是否工作(只是为了确定!):

$('#btnupdate').click(function(){alert("是的,至少按下了按钮");});

更新

如果脚本被执行,则开始测试:

alert("yes at least the script gets executed");
$('#btnupdate').click(function() { alert("yes at least the button was pressed"); });

如果没有,您一定在某个地方有javascript错误。https://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers

如果是,您的按钮不会被JQuery捕获(不知道为什么)

无论如何,它与ajax或get无关!