使用SweetAlert删除记录将不起作用

时间:2017-05-17 13:09:17

标签: javascript php jquery mysql

你能告诉我这段代码有什么问题吗?我正在使用ajax,php和mysql显示一个表。我正在使用X-editable库进行内联编辑。我的问题是当我点击删除按钮弹出删除确认模式时,但当我确认我要删除一行/记录时,我什么都没得到。

我已经检查了我的日志文件和js控制台,但我没有看到任何明显的错误。我知道我的代码问题很少。这就是我的代码看起来的样子。你能检查一下我的错误吗?

这是我的脚本文件:

<script>
   // this function is used to display the table
   function fetch_term(term) {
      // body...
      if (term != '') {
        $.ajax({
          url:"includes/ajax/fetch_term_grades.php",
          method:"post",
          data:{"term":term},
          dataType:"text",
          success:function(data){
            $("#result").html(data);               
            $('#dataTable').DataTable();
            //$('table').attr('id', 'dataTable');  
          }
      });
     } else {
        $('#result').html('');
     }
   }

// this is used to delete records    
function swal_delete(id){
   swal({
       title: 'Are you sure?',
       text: "You won't be able to revert this!",
       type: 'warning',
       showCancelButton: true,
       confirmButtonColor: '#3085d6',
       cancelButtonColor: '#d33',
       confirmButtonText: 'Yes, delete it!',
       showLoaderOnConfirm: true,
        preConfirm: function() {
            return new Promise(function(resolve) {
              $.ajax({
                 url: 'delete_score.php',
                 type: 'POST',
                 data: 'delete='+id,
                 dataType: 'json'
              })
              .done(function(response){
                 swal('Deleted!', response.message, response.status);
                 fetch_term(term);
                })
              .fail(function(){
                 swal('Oops...', 'Something went wrong with ajax !', 'error');
              });
            });
        },
        allowOutsideClick: false
    });
}

$(document).ready(function() {

    var term = $('#term').val();


    fetch_term(term);


    $('#term').on('change', function() {
        var changeTermVal = $('#term').val();
        fetch_term(changeTermVal);
    }); 

    $(document).on('click', '#delete_score', function(event) {
        /* Act on the event */
        var id = $(this).data('id');
        swal_delete(id);

        event.preventDefault();


    });
});




</script>

这是返回要显示的表的文件(fetch_term_grades.php)

<?php 
$term = mysqli_escape_string($connection, $_POST['term']);
$result = term_select_all($term);

$output = '';
if (mysqli_num_rows($result) > 0) {
    # code...
    $output .= '<table class="table table-bordered" id="dataTable">
                    <thead>
                        <tr>
                            <th>Student Name</th>
                            <th>Subject</th>
                            <th>Class</th>
                            <th>Score</th>
                            <th>Action</th>
                        </tr>
                    </thead>';
                    $output .= '<tbody>';
                        while ($row = mysqli_fetch_assoc($result)) {
                                $output .= '<tr>';
                                    $output .= '<td>'.$row["first_name"]." ".$row["middle_name"]." ".$row["surname"].'</td>';
                                    $output .= '<td>'.$row["subject_name"].'</td>';
                                    $output .= '<td>'.$row["class_name"].'</td>';
                                    $output .= '<td>'.$row["score"].'</td>';
                                    $output .= '<td>';
                                        $output .= '<a class="btn btn-info btn-sm" href="edit_grade.php?student='.$row["id"].'" data-target="#scoreModal" data-toggle="modal" data-backdrop="static" data-keyboard="false">';
                                            $output .= '<i class="fa fa-edit"></i>';
                                        $output .= '</a>';
                                        $output .= '&nbsp;';
                                        $output .= '<a class="btn btn-danger btn-sm" id="delete_score" data-id="'.$row["id"].'" href="javascript:void(0)">';
                                            $output .= '<i class="fa fa-trash"></i>';
                                        $output .= '</a>';
                                    $output .= '</td>'; 
                                $output .= '</tr>';
                        }
                    $output .= '</tbody>';
    $output .= '</table>';
    echo $output;
} else {
    echo "Data not found";
}

&GT;

这是用于删除记录的文件。

<?php
$response = array();

$id = intval($_POST['delete']);
var_dump($id);

$sql = "DELETE from period_one WHERE id = {$id} LIMIT 1 ";

$result = mysqli_query($connection, $sql);


if($result && mysqli_affected_rows($connection) == 1){
    $response['status']  = 'success';
    $response['message'] = 'Product Deleted Successfully ...';
}
else {
    $response['status']  = 'error';
    $response['message'] = 'Unable to delete product ...';
}

echo json_encode($response); 

&GT;

0 个答案:

没有答案