CI中未更新数据

时间:2018-03-20 07:13:08

标签: php codeigniter

我正在设计CI应用程序并陷入ajax查询。基本上我写的函数是当按下保存按钮时将id作为null,即使我点击编辑按钮它显示它拾取正确的id。看起来我在功能上有一些错误。以下是我写的功能:

public function ajax_update()
    {
        $this->_validate();
        $data = array(
                //'firstName' => $this->input->post('firstName'),
                //'lastName' => $this->input->post('lastName'),
                //'gender' => $this->input->post('gender'),
                //'address' => $this->input->post('address'),
                //'dob' => $this->input->post('dob'),
                //'tid' => $this->input->post('tid'),
                'name' => $this->input->post('tname'),
            );


        $this->transport->update(array('tid' => $this->input->post('tid')), $data);
        var_dump( $this->input->post());
        echo json_encode(array("status" => TRUE));
    }

更新功能是

public function update($where, $data)
    {
        $this->db->update($this->table, $data, $where);
        return $this->db->affected_rows();
    }

查看

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><html lang="en">
    <head>
        <?php include_once("header.php"); ?>
    </head>
    <body class="fixed-nav sticky-footer bg-dark">
          <!-- Navigation-->
         <?php include_once("sidebar.php"); ?>
        <div>
            <div class="content-wrapper">
                <div class="container-fluid">
                  <!-- Breadcrumbs-->
                  <ol class="breadcrumb">
                    <li class="breadcrumb-item">
                      <a href="#">Home</a>
                    </li>
                    <li class="breadcrumb-item active">Manage Transport</li>
                  </ol>
                  <!--Button to add Client
                  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal2"><i class="fa fa-plus" style="color:white"></i> Add Transport</button>-->
                  <button class="btn btn-success" onclick="add_transport()"><i class="glyphicon glyphicon-plus"></i> Add Transport</button>
                  <br>
                  <br>
                  <!-- Example DataTables Card-->
                  <div class="card mb-3">
                    <div class="card-header">
                      <i class="fa fa-table"></i> View Transport Details</div>
                    <div class="card-body">
                      <div class="table-responsive">
                        <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                          <thead>
                            <tr>
                              <th style="width:90%;">Transport Detail</th>
                              <th>Action</th>
                            </tr>
                          </thead>
                          <tfoot>
                            <tr>
                              <th>Transport Detail</th>
                              <th>Action</th>
                            </tr>
                          </tfoot>
                          <tbody>
                          </tbody>
                        </table>
                      </div>
                    </div>
                  </div>
                </div>
            <!-- /.container-fluid-->
            <!-- /.content-wrapper-->
            </div>



            <!-- Modal to add Transport-->
             <div class="modal fade" id="myModal" role="dialog">
                <div class="modal-dialog">
                  <!-- Modal content-->
                  <div class="modal-content">
                    <div class="modal-header">
                      <h4 class="modal-title">Add Transport Details</h4>
                      <button type="button" class="close" data-dismiss="modal">x</button>
                    </div>
                    <div class="modal-body">
                     <!--<form role="form" name="form1" action="<?php echo base_url('search/add_trans'); ?>" method="post"  autocomplete="on">-->
                     <form action="#" id="form" class="form-horizontal">
                      <div class="row">
                        <div class="col-md-2">
                            <label>Transport Details</label>
                        </div>
                        <div class="col-md-10" id="new_data">
                            <textarea  name="tname" class="form-control" placeholder="Transport Name" rows="5" value=""></textarea>
                        </div>
                      </div>       
                      </form>
                    </div>
                    <div class="modal-footer">
                      <!--<input type="submit" name="submit" class="btn btn-primary" value="Submit">-->
                      <button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Save</button>
                      <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                    </div>
                  </div>
                </div>
              </div>
        <!-- Modal Finishes-->  
        </div>
        <?php  include_once 'footer.php'; ?>
        <script type="text/javascript">
            var save_method; //for save method string
            var table;

             //set input/textarea/select event when change value, remove class error and remove text help block 
                $("input").change(function(){
                    $(this).parent().parent().removeClass('has-error');
                    $(this).next().empty();
                });
                $("textarea").change(function(){
                    $(this).parent().parent().removeClass('has-error');
                    $(this).next().empty();
                });
                $("select").change(function(){
                    $(this).parent().parent().removeClass('has-error');
                    $(this).next().empty();
                });

                function add_transport()
                {
                    save_method = 'add';
                    $('#form')[0].reset(); // reset form on modals
                    $('.form-group').removeClass('has-error'); // clear error class
                    $('.help-block').empty(); // clear error string
                    $('#myModal').modal('show'); // show bootstrap modal
                    $('.modal-title').text('Add Transport'); // Set Title to Bootstrap modal title
                }

                function edit_transport(id)
                {
                    //var table = $('#dataTable').DataTable();
                   //  console.log( table.row( id ).data() );
                 //    $("#tid").val(data.tname);
                    save_method = 'update';
                    $('#form')[0].reset(); // reset form on modals
                    $('.form-group').removeClass('has-error'); // clear error class
                    $('.help-block').empty(); // clear error string

                    //Ajax Load data from ajax
                    $.ajax({
                        url : "<?php echo site_url('transport/ajax_edit/')?>/" + id,
                        type: "GET",
                        dataType: "JSON",
                        success: function(data)
                        {

                           // $('[name="id"]').val(data.id);
                                //$('[name="tid"]').val(data.tid);
                                $('[name="tname"]').val(data.tname);
                           // $('[name="firstName"]').val(data.firstName);
                            //$("#tid").val(data.tname);
                           //alert(data.tname);
                           // $('[name="lastName"]').val(data.lastName);
                           // $('[name="gender"]').val(data.gender);
                           // $('[name="address"]').val(data.address);
                          //  $('[name="dob"]').datepicker('update',data.dob);
                          //  $('#modal_form').modal('show'); // show bootstrap modal when complete loaded
                              $('#myModal').modal('show'); // show bootstrap modal when complete loaded
                            $('.modal-title').text('Edit Transport'); // Set title to Bootstrap modal title
                        //  new_data
                        //$("#new_data").html('<textarea name="name" class="form-control" placeholder="Transport Name" rows="5" value=""></textarea> <input type="text" name="row_id" value="'+id+'" readonly hidden  >');

                        },
                        error: function (jqXHR, textStatus, errorThrown)
                        {
                            alert('Error get data from ajax');
                        }
                    });
                }

                //function reload_table()
                //{
                //   table.ajax.reload(null,false); //reload datatable ajax 
                //}
                function reload_table() {
                table.api().ajax.reload(null, false); //reload datatable ajax 
            }

                function save()
                {
                    $('#btnSave').text('saving...'); //change button text
                    $('#btnSave').attr('disabled',true); //set button disable 
                    var url;

                    if(save_method == 'add') {
                        url = "<?php echo site_url('transport/ajax_add')?>";
                    } else {
                        url = "<?php echo site_url('transport/ajax_update')?>";
                    }
                    // console.log($('#form').serialize());
                    // ajax adding data to database
                    $.ajax({
                        url : url,
                        type: "POST",
                        data: $('#form').serialize(),
                        dataType: "JSON",
                        success: function(data)
                        {

                            if(data.status) //if success close modal and reload ajax table
                            {
                                $('#myModal').modal('hide');
                                reload_table();
                            }
                            else
                            {
                                for (var i = 0; i < data.inputerror.length; i++) 
                                {
                                    $('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
                                    $('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string
                                }
                            }
                            $('#btnSave').text('save'); //change button text
                            $('#btnSave').attr('disabled',false); //set button enable 
                            //$("#new_data").html('<textarea name="name" class="form-control" placeholder="Transport Name" rows="5" value=""></textarea> ');
                            reload_table();
                        },
                        error: function (jqXHR, textStatus, errorThrown)
                        {
                            alert('Error adding / update data');
                            $('#btnSave').text('save'); //change button text
                            $('#btnSave').attr('disabled',false); //set button enable 
                            //$("#new_data").html('<textarea name="name" class="form-control" placeholder="Transport Name" rows="5" value=""></textarea>');

                        }
                    });
                }

                function delete_transport(id)
                {
                    if(confirm('Are you sure delete this data?'))
                    {
                        // ajax delete data to database
                        $.ajax({
                            url : "<?php echo site_url('transport/ajax_delete')?>/"+id,
                            type: "POST",
                            dataType: "JSON",
                            success: function(data)
                            {
                                //if success reload ajax table
                                $('#myModal').modal('hide');
                                reload_table();
                            },
                            error: function (jqXHR, textStatus, errorThrown)
                            {
                                alert('Error deleting data');
                            }
                        });

                    }
                }

                function reload_table()
                {
                //console.log(table);
                 table.api().ajax.reload( null, false );
                }

                $(document).ready(function() {

                    //datatables
                    table = $('#dataTable').dataTable({ 

                        "processing": true, //Feature control the processing indicator.
                        "serverSide": true, //Feature control DataTables' server-side processing mode.
                        "order": [], //Initial no order.

                        // Load data for the table's content from an Ajax source
                        "ajax": {
                            "url": "<?php echo site_url('transport/ajax_list')?>",
                            "type": "POST"
                        },

                        //Set column definition initialisation properties.
                        "columnDefs": [
                        { 
                            "targets": [ -1 ], //last column
                            "orderable": false, //set not orderable
                        },
                        ],

                    });


            });

        </script>
    </body>
</html>

请指点什么?

0 个答案:

没有答案