简体   繁体   English

Ajax没有更新数据

[英]Ajax is not updating data

I've got a forum in which user is allowed to edit and delete only his comments, I've defined an "edit" button, that by a click of mouse brings down a modal, and in that modal user is allowed to get access to the data's he/she has been sent before, I've written an ajax to target these field and update them whenever the users clicks on "edit" button, code totally makes sense, but so far the functionality doesn't, to make it more clear, user clicks, modal comes down, whatever he/she has been posted will appear in fields, and there is an "edit" button at the bottom of modal, which is responsible for changing and updating data. 我有一个论坛,在该论坛中,允许用户仅编辑和删除他的评论,我定义了一个“编辑”按钮,单击鼠标即可放下模式,在该模式下,用户可以访问对于以前发送过的数据,我编写了一个ajax来定位这些字段,并在用户单击“编辑”按钮时对其进行更新,代码完全有意义,但到目前为止该功能还没有实现更清楚的是,用户单击,模态下降,他/她发布的任何内容都将出现在字段中,模态底部有一个“编辑”按钮,用于更改和更新数据。 here is the modal code : 这是模态代码:

                <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>

as you can see I've assigned "editted" to my "name" attribute, which is later on used to call the query in the database, sql code is as below : 如您所见,我已将“ 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>' ; 


            }

and here is the ajax code snippet; 这是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);
});

I assume there is nothing advance about this segment of code, and i'm missing something very simple, any consideration is highly appreciated :) 我认为这部分代码没有任何进展,并且我缺少一些非常简单的东西,任何考虑都值得赞赏:)

This (untested code) may be similar to what you should do: 此(未经测试的代码)可能与您应该执行的操作类似:

$('#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);
    });
});

UPDATE 更新

Try ajax instead of get to see if some error occurs in the loading 尝试使用ajax而不是查看加载是否发生了一些错误

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

UPDATE 更新

Start testing if the click handler works then (just to be sure!): 然后开始测试点击处理程序是否起作用(请确定!):

$('#btnupdate').click(function() { alert("yes at least the button was pressed"); }); $('#btnupdate')。click(function(){alert(“是的,至少按下了按钮”);});

UPDATE 更新

Start testing if the script gets executed then: 开始测试脚本是否被执行,然后:

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

If not you must have a javascript error somewhere. 如果不是,那么您必须在某个地方出现JavaScript错误。 https://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers https://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers

If yes, your button does not get caught by JQuery (no idea why) 如果是,则您的按钮不会被JQuery捕获(不知道为什么)

anyway it's got nothing to do with ajax or get! 无论如何,它与ajax无关。

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

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