简体   繁体   English

在表行上编辑数据并使用Ajax保存在数据库中

[英]Edit data on table row and save in database with ajax

I'm not very good(not at all) with ajax/js and will need a little bit help here. 我对ajax / js不太好(一点也不),在这里需要一点帮助。

I have table on page which I show data like this: 我在页面上有表格,其中显示了如下数据:

    <table class="table table-bordered">
        <thead>
            <tr>
            <th>People</th>
            <th>Rate per person</th>
            <th width="200px">Action</th>
            </tr>
        </thead>
        <tbody>
            <?php 
        $result_rates = $conn->query("SELECT * FROM rates Where user_id = 60");
        if($result_rates->num_rows>0){
            while ($row=$result_rates->fetch_all(MYSQLI_ASSOC)) {
                foreach ($row as $r){

                }
            }
        }
        ?> 
        <tr>
           <td>1</td>

           <td >$ <?php if($r['rate_one'] > 0){echo $r['rate_one'];}else{echo '0';} ?></td>
           <td><button data-toggle="modal" data-target="#edit-item" class="btn btn-primary edit-item">Edit</button></td>

        </tr>
           <tr>
               <td>2</td>
               <td>$ <?php if($r['rate_two'] > 0){echo $r['rate_two'];}else{echo '0';} ?></td>
               <td><button data-toggle="modal" data-target="#edit-item" class="btn btn-primary edit-item">Edit</button></td>
           </tr>

I have also this modal on the Edit button click 我也在“ Edit按钮上单击了此模式

    <!-- Edit Item Modal -->
    <div class="modal fade" id="edit-item" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
            <h4 class="modal-title" id="myModalLabel">Edit Item</h4>
          </div>

          <div class="modal-body">
                <form  action="includes/update.php" method="put">
                    <input type="hidden" name="<?php echo $r['rate_id']; ?>" class="edit-id">

                    <div class="form-group">
                        <label class="control-label" for="title">People:</label>
                        <input type="text" name="rate_one" class="form-control" />
                    </div>

                    <div class="form-group">
                        <label class="control-label" for="title">Prise:</label>
                        <input type="text" name="rate_two" class="form-control" />
                    </div>

                    <div class="form-group">
                        <button type="submit" class="btn btn-success crud-submit-edit">Submit</button>
                    </div>

                </form>

          </div>
        </div>
      </div>
    </div>

So when I click on Edit button modal is opened and the data is loaded. 因此,当我单击“编辑”按钮时,将打开模态并加载数据。 If I change some data and click on Submit I've got message for success but data isn't changed in database. 如果我更改了一些数据,然后单击“ Submit ,则会收到成功消息,但数据库中的数据未更改。

This is the ajax part 这是ajax部分

/* Edit Item */
$("body").on("click",".edit-item",function(){

    var id = $(this).parent("td").data('id');
    var rate_one = $(this).parent("td").prev("td").prev("td").text();
    var rate_two = $(this).parent("td").prev("td").text();

    $("#edit-item").find("input[name='rate_one']").val(rate_one);
    $("#edit-item").find("input[name='rate_two']").val(rate_two);
    $("#edit-item").find(".edit-id").val(id);

});

/* Updated new Item */
$(".crud-submit-edit").click(function(e){

    e.preventDefault();
    var form_action = $("#edit-item").find("form").attr("action");
    var rate_one = $("#edit-item").find("input[name='rate_one']").val();

    var rate_two = $("#edit-item").find("input[name='rate_two']").val();
    var rate_id = $("#edit-item").find(".edit-id").val();

        $.ajax({
            dataType: 'json',
            type:'POST',
            url: form_action,
            data:{rate_one:rate_one, rate_two:rate_two,rate_id:rate_id}
        }).done(function(data){
            getPageData();
            $(".modal").modal('hide');
            toastr.success('Rate Updated Successfully.', 'Success Alert', {timeOut: 5000});
        });  
});

When I look in dev console I see that rate_id isn't passed too. 当我在开发者控制台中查看时,我也看到rate_id也未传递。 Can anyone help a bit? 有人可以帮忙吗?

  $id  = $_POST["rate_id"];
  $post = $_POST;

  $sql = "UPDATE rates SET rate_one = '".$post['rate_one']."'
    ,rate_two = '".$post['rate_two']."' 
    WHERE rate_id = '".$id."'";

it is for testing purposes and thats why it isn't with prepared statements 这是出于测试目的,这就是为什么它不包含准备好的语句

In form data you are not setting rate id in value. 在表单数据中,您未在值中设置费率ID。 Please do the below changes. 请进行以下更改。

<input type="hidden" name="edit-id" class="edit-id" value=<?php echo $r['rate_id']; ?>>

and also please share the php code of update.php for better help. 并且还请共享update.php的php代码以获得更好的帮助。

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

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