Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
成功php的AJAX::删除选中表列表::使用超链接不输入_Php_Mysql_Ajax_Delete Row_Sql Delete - Fatal编程技术网

成功php的AJAX::删除选中表列表::使用超链接不输入

成功php的AJAX::删除选中表列表::使用超链接不输入,php,mysql,ajax,delete-row,sql-delete,Php,Mysql,Ajax,Delete Row,Sql Delete,项目焦点:从表单中删除多检查表列表。 规格: 1.)删除操作通过使用超链接(而不是通过翻阅大量书签),我找到了一个我希望实现的示例。在处理代码片段后,我终于让ajax按照我希望在项目的这一步中实现的方式工作。 为了帮助其他人进行搜索,我在下面提供了ajax/jq/js和php的所有代码,这些代码在我的测试中完美无瑕。 这段代码是如何工作的 按钮(表单外部的超链接,不是输入按钮)链接到deletedRecord.php,但脚本使用e.preventDefault()覆盖链接 JQ用于构建选中行i

项目焦点:从表单中删除多检查表列表。
规格:

1.)删除操作通过使用超链接(而不是通过翻阅大量书签),我找到了一个我希望实现的示例。在处理代码片段后,我终于让ajax按照我希望在项目的这一步中实现的方式工作。

为了帮助其他人进行搜索,我在下面提供了ajax/jq/js和php的所有代码,这些代码在我的测试中完美无瑕。

这段代码是如何工作的
  • 按钮(表单外部的超链接
    不是输入按钮
    )链接到
    deletedRecord.php
    ,但脚本使用
    e.preventDefault()覆盖链接
  • JQ用于构建选中行id的数组&通过AJAX将它们发送到
    deletedRecord.php
  • deleteRecord.php
    分解数组,计算检查的ID总数,最后循环查询以删除每个ID
  • 成功完成后,回显
    1
    响应以触发成功操作
希望这对其他人有所帮助。如果有人看到我可能错过的任何其他错误,请随意分享,以获得更大的好处。干杯

ajaxDelete.js 备注:
1.)将图像(按钮)
href
更新为
href=“deleteRecord.php”

2.)研究并发现了一种更好的方法,可以将计数减少到只检查的(我认为如果表增长到大量行,这种方法会更有效(更快)

$(document).ready(function() {
$('#btn_del').click(function(e) {
e.preventDefault(); 
    page    = $(this).attr("href");
    ids     = new Array()
    a       = 0;

    $(".chk:checked").each(function(){
       ids[a] = $(this).val();
       a++;
})   
      // alert(ids);

if (confirm("Are you sure you want to delete these courses?")) {

     $.ajax({
            url         :   page,
            type        :   "POST",
            data        :   "id="+ids,
            dataType    :   'json',
            success     :   function(res) {
                             if ( res == 1 ) {
                                 $(".chk:checked").each(function() {
                                     $(this).parent().parent().remove();
                                  })        // end if then remove table row
                                }           // end if res ==1
                             }              // end success response function
            })                              // end ajax
     }                                      // end confirmed
return false;
});                                         // end button click function
});                                         // end doc ready
deleteRecord.php
您收到的错误是什么?第一个错误告诉我您在php脚本中声明的变量ajaxis
$count
中的
var count
写错了?@Ohgodwhy yes
$count=mysqli_num_rows($result)
成功地生成了一个精确的计数。您将在3分钟内被破解,请不要在您的代码中使用这种逻辑
$chkbx=$\u POST['chkbx'];$del\u id=$chkbx[$i];其中unit\u id='“$del\u id.”“;
搜索sql注入
<li class="button" id="toolbar-del">
    <a href="#" title="Delete" id="btn_del">
        <span class="icon-16-delete dead"></span>
        Delete
    </a>
</li>
<?
                                // Check if DELETE button active, start this
$delete         = $_POST['delete'];
$chkbx          = $_POST['chkbx'];

if($delete){
 for($i=0;$i<$count;$i++){
   $del_id  = $chkbx[$i];
   $sql     = "DELETE FROM ".ID_TABLE." WHERE unit_id='".$del_id."'";
   $result  = mysqli_query($dbc,$sql);
 }
                                // if successful redirect to delete_multiple.php
 if($result){
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=records_manager.php\">";
    }else{
        echo "Error: No luck";
    }
  }
mysqli_close($dbc);
?>
// ajaxDelete.js
$(document).ready(function() {
                                                    // When TRASH button is clicked...
$('#btn_del').click(function(event) {

    e.preventDefault();                             // stop the form submitting the normal way
                                                    // and refreshing the page  

    // Get the form data                            // there are many ways to get this data using jQuery
    // ----------------------------------           // (you can use the class or id also)
    var formData = {
              'chkbx'   : $('input[name=chkbx]').val(),
              'count'   : $count[0]

    // Process the form
    // ================
    $.ajax({
              type      : 'POST',                   // define the type of HTTP verb we want to use
              url       : 'deleteRecord.php',       // the url where we want to POST
              data      : formData,                 // our data object
              dataType  : 'json',                   // what type of data do we expect back from the server
              encode    : true
          })

                                                    // using the .done(), 
                                                    // promise callback
    .done(function(data) {                      

        window.console.log(data);                   // log data to the console so we can see

        // Handle ERRORS
        if ( ! data.success) {                                      

            if (data.errors.chkbx) {                
                $('.Records_Found').addClass('has-error');
                $('.Records_Found').append('<div class="help-block">'+ data.errors.chkbx + '</div>');   
            }
        }                                           // end if ERRORS
        else {
            $('.Records_Found').append('<div class="alert alert-success" id="valid_success">'+ data.message + '</div>');
    // After form submission,
                                                    // redirect a user to another page
        window.location = 'records_manager.php'; 
              }
          })

          .fail(function(data) {                    // promise callback
          window.console.log(data);  });            // show any errors in console
                                                    // NOTE: it's best to remove for production

          event.preventDefault();                   // stop the form from submitting the normal way
                                                    // and refreshing the page  
      });                                           // end submit button

    });                                             // end document ready
<?php

// FUNCTION to DELETE
// ===========================
// :checked existing unit data

$errors = array();                      // array to hold validation errors
$data   = array();                      // array to pass back data

if ( empty($_POST['chkbx']))                // if empty, populate error
    $errors['chkbx'] = 'No items have been checked yet.';
// ERROR! Return a response
if ( ! empty($errors)) {        

    $data['success'] = false;           // any errors = return a success boolean of FALSE
    $data['errors']  = $errors;         // return those errors

} else {                            

// NO ERROR... Carry on                     // Process the form data  
    require_once('config.php');             // Connect to the database

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
        or die ('Error connecting to MySQL server.'.$dbc);
                                            // Check if DELETE 
$delete         = $_POST['delete'];
$chkbx          = $_POST['chkbx'];
$count          = $_POST['count'];

if($delete){
    for($i=0;$i<$count;$i++){
        $del_id     = $chkbx[$i];
        $sql        = "DELETE FROM ".ID_TABLE." WHERE unit_id='".$del_id."'";
        $result     = mysqli_query($dbc,$sql);
    }
                                            // if successful redirect
    if($result){
            echo "<meta http-equiv=\"refresh\" content=\"0;URL=records_manager.php\">";
    }else{
            echo "Error: No luck";
    }
}
mysqli_close($dbc);                             // close DB connection
}
$data['success'] = true;                        // show a message of success 
$data['message'] = 'Success!';                  // and provide a true success variable
}
echo json_encode($data);                        // return all our data to an AJAX call  
}                                               // end else NO ERRORS, process form 
?>
$(document).ready(function() {
$('#btn_del').click(function(e) {
e.preventDefault(); 
    page    = $(this).attr("href");
    ids     = new Array()
    a       = 0;

    $(".chk:checked").each(function(){
       ids[a] = $(this).val();
       a++;
})   
      // alert(ids);

if (confirm("Are you sure you want to delete these courses?")) {

     $.ajax({
            url         :   page,
            type        :   "POST",
            data        :   "id="+ids,
            dataType    :   'json',
            success     :   function(res) {
                             if ( res == 1 ) {
                                 $(".chk:checked").each(function() {
                                     $(this).parent().parent().remove();
                                  })        // end if then remove table row
                                }           // end if res ==1
                             }              // end success response function
            })                              // end ajax
     }                                      // end confirmed
return false;
});                                         // end button click function
});                                         // end doc ready
<?php
require_once('config.php');                 // Connect to the database

    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
        or die ('Error connecting to MySQL server.'.$dbc);

    $del_id = explode(",",$_POST['id']);
    $count = count($del_id);
        if (count($count) > 0) {
            foreach ($del_id as $id) {
                      $sql = "DELETE FROM ".ID_TABLE."
                              WHERE unit_id='" . $id . "'";
                      $result = mysqli_query($dbc,$sql) 
                              or die(mysqli_error($dbc)); 
            }                                   // end for each as

        mysqli_close($dbc);                     // close MySQL

        echo json_encode(1);                    // Return json res == 1
                                                // this is used for the SUCCESS action
        }                                       // end if count > 0
?>