php
  • jquery
  • checkbox
  • 2016-07-18 75 views 0 likes 
    0

    我的代碼是checkall後我檢查的最上部複選框,然後刪除所有選中的複選框,當我碰杯刪除所有按鈕不能全部刪除如果複選框全部是檢查

    我的問題是我不能刪除選中的複選框

    我想刪除選中的行

    這裏是我的視圖頁面

    jQuery(".checkall").on("click",function(){ 
           var checkall = jQuery(this); 
           jQuery(".rows input[type='checkbox']").each(function(){ 
            var rows_this = jQuery(this); 
            var row_id = rows_this.attr("data-id"); 
    
            var parent = rows_this.closest("tr"); 
            var view_button = parent.find(".view"); 
            var href = "personal_information.php?id=" +row_id; 
            var delete_button = parent.find(".delete"); 
            var href1 = "delete.php?id=" +row_id; 
    
             if(checkall.is(":checked")){ 
              rows_this.prop("checked",true); 
              view_button.removeClass("color"); 
              view_button.attr("href",href); 
              delete_button.attr("href",href1); 
              delete_button.removeClass("gray"); 
             }else{ 
              rows_this.prop("checked",false); 
              view_button.addClass("color"); 
              view_button.attr("href","javascript:void(0);"); 
              delete_button.attr("href","javascript:void(0);"); 
              delete_button.addClass("gray"); 
             } 
            }); 
           }); 
    

    的d這裏是我的jQuery的

    <table><form> 
        <tr> 
         <th><input type="checkbox" class="checkall"></th> 
         <th style="width:100px;"><center>Last Name</center></th> 
         <th style="width:100px;"><center>First Name</center></th> 
         <th style="width:auto;"><center>Email</center></th> 
         <th style="width:100px;"><center>Birthday</center></th> 
         <th style="width:auto;"><center>Action</center></th> 
        </tr> 
        <tr> 
        <?php 
        while($row = mysql_fetch_array($result)) 
        { 
         echo "<tr class='rows' id='".$row['id']."'>"; 
         echo "<td>"; 
         echo "<center><input type='checkbox' data-id='".$row['id']."'></center>"; // data-id the id per row 
         echo "<td>".$row['lastname']."</td>"; 
         echo "<td>".$row['firstname']."</td>"; 
         echo "<td>".$row['email']."</td>"; 
         echo "<td>".$row['bdate']."</td>"; 
         echo "<td><center>"; 
         echo "<a href='javascript:void(0);' style='text-decoration:none' class='btn delete gray'>DELETE</a> "; 
         echo "<a href='javascript:void(0);' style='text-decoration:none' class='btn view color'>VIEW</a> "; 
         echo "</td></center></td>"; 
         echo "</tr>"; 
    
        } 
        ?> 
        </tr> 
        <tr><td> 
        <input type="submit" value="Delete" class="deleteall"> 
        </td></tr> 
    </form></table> 
    

    這是我刪除頁面PHP

    <?php 
    include 'dbconfig.php'; 
    
    $id = $_GET['id']; 
    
    $sql = "DELETE FROM user WHERE id =".$id; 
    mysql_query($sql); 
    Header("Location: user.php"); 
    ?> 
    
    +0

    是否顯示任何錯誤? – dod29

    +0

    @ dod29沒有顯示錯誤的錯誤 – reyjoel

    +0

    @ dod29它只是不刪除所有檢查過的框 – reyjoel

    回答

    1

    你需要有一個name屬性將數據發送到後端:

    echo "<center><input type='checkbox' name='id[]' data-id='".$row['id']."'></center>"; 
    // and BTW `<center>` tag is removed in HTML5, so better to look for other way 
    

    和在deleteall按鈕上添加名稱屬性:

    <input type="submit" name='deleteall' value="Delete" class="deleteall"> 
    

    現在你可以在表格上添加一些屬性:

    <form action='delete.php' method='get'> 
    

    ,併爲您發送確認值的數組:

    if(isset($_GET['deleteall'])){ // check if form is submitted using deleteall button 
        $id = $_GET['id']; // get the array 
        for($i=0;$i<count($id);$i++){ // loop through each ids in the array 
    
         $id = $id[$i]; /// get the id in the iteration 
         $sql = "DELETE FROM user WHERE id=".$id; // make the query 
         $result = mysqli_query($sql); // execute to delete 
    
        } 
    
        Header("Location: user.php"); // when all deleted then navigate 
    
    } 
    

    以及每個你的評論可以使用jQuery將所有選中的複選框發送回服務器:

    $('.deleteall').click(function(e){ 
        e.preventDefault(); // <---use this stop the form submission. 
    
        var arr2send = $('.rows input[type=checkbox]:checked').map(function(){ 
         return $(this).data('id'); 
        }).get(); 
    
        var deleteallurl = 'delete.php?id='+JSON.stringify(arr2send); 
    
        window.location.href = deleteallurl; 
    
    }); 
    

    然而,您需要在後端循環遍歷數組中的所有ID並刪除它們,並且在刪除所有刪除後,您可以導航回user.php頁面。

    +0

    謝謝先生,但jquery需要刪除所有ID – reyjoel

    +0

    因此,你不想更新你的視圖,jQuery在這種情況下必須做什麼?你能更多地關注它究竟需要什麼嗎? – Jai

    +0

    @reyjoel檢查更新是否有幫助。 – Jai

    0

    SQL Injection Alert !!

    即使你的代碼是不工作:

    $sql = "DELETE FROM user WHERE id =".$id; 
    

    不好的做法

    你在問SQL注入,那是你不想要的東西!即使腳本小子可以刪除您的數據庫等。

    請檢查:https://www.owasp.org/index.php/SQL_Injection,至少爲了您自己的利益作爲開發者!


    除此之外: 始終放置一個exitHeader("Location: ...)

    檢查http://php.net/manual/en/function.header.php

    相關問題