繁体   English   中英

Ajax没有更新数据

[英]Ajax is not updating data

我有一个论坛,在该论坛中,允许用户仅编辑和删除他的评论,我定义了一个“编辑”按钮,单击鼠标即可放下模式,在该模式下,用户可以访问对于以前发送过的数据,我编写了一个ajax来定位这些字段,并在用户单击“编辑”按钮时对其进行更新,代码完全有意义,但到目前为止该功能还没有实现更清楚的是,用户单击,模态下降,他/她发布的任何内容都将出现在字段中,模态底部有一个“编辑”按钮,用于更改和更新数据。 这是模态代码:

                <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无关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM