Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts

    Check Box delete weirdness

    I've set up an indented list created with a standard parent/child script to reveal user assignments to web pages. In the edit users page we can delete assignments to the list by checking boxes and running the delete form.

    Here's the odd behavior: When I remove the indenting from the list I can select any check box or multiple check boxes and the access is removed. However, if I implement the indenting using the code below when I check a box the next box in the list is deleted. If there's a child in the list and I try and delete the child nothing happens.

    For example if the list is:
    • Page 1
    • Page 2
      • Page 4
    • Page 3


    When I select Page 4 to delete nothing happens. If I select Page 2 and delete then Page 1 is removed.

    Here's the code:
    PHP Code:
    <?php
    // Check if bizCat delete button active, start this 
    if(isset($_POST['delete']) && $_POST['checkbox'] <> NULL){
        
    $ids implode(', '$_POST['checkbox']);
        
    $sql "DELETE FROM userBizCats WHERE id IN($ids)";
        
    $result mysql_query($sql);        
        if(
    $result){    
            
    $pgID $row_pageRS['id'];
            
    header("location: docsTest.php?recordID=$pgID"); 
            exit;
        }
    }
    ?>


     // -------- The code above is before the HTML, the code below is in a div tag in the body ----------
    <?php
        
    echo '<form name "deleteForm" method="post" id="deleteForm">';
        echo 
    '<p><input name="delete" type="submit" id="delete" formmethod="POST" value="Delete Selected BicCats" /></p>';

        
    $parentid 0// assuming that 0 is the main category.
        
    get_sub_catsBC($parentid);
        function 
    get_sub_catsBC($parentid) {
        
        
    $sql "SELECT
        bizCats.id AS bcSN, bizCats.bcID, bizCats.bcName,
        userBizCats.bizCatID,
        users.id 
        FROM bizCats, userBizCats, users
        WHERE bizCats.bcID = "
    .$parentid."
        AND userBizCats.bizCatID = bizCats.id
        AND users.id = '35'
        "

        
    $result mysql_query($sql);
        echo 
    '<ul class="aqtree3clickable">';
            if (
    mysql_num_rows($result) > 0) {
                while (
    $rec mysql_fetch_assoc($result)) {
                    
    $delID $rec['bcSN'];
                    
                    echo 
    '<input type="checkbox" name="checkbox[]" id="checkbox[]"  value='$delID '>SN: ',  $delID' - '$rec['bcName']; 
            
    get_sub_catsBC($rec['bcSN']);
                }
            }
        echo 
    '</ul>';
        echo 
    '</form>';
        }
       
    ?>
    Simple removing the get_sub_catsBC and parents ID lines from the code produces a non indented list and then all the delete functions work just fine.

    I hope that I'm not completely barking up the wrong tree here....
    Last edited by rgEffects; 01-04-2013 at 01:52 AM. Reason: Found the solution....

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    PHP Code:
    echo <input type="checkbox" name="checkbox[]" id="checkbox[]" 
    Ids must be unique on the page and it is recommended that you do not use square brackets as part of an id.

    It appears, also, that you are inserting the checkboxes directly into a UL. ULs should only contain LIs, but you can insert each checkbox into an LI if you wish.

    Added: Your echo looks like it might create incorrect HTML:

    PHP Code:
    echo '<input type="checkbox" name="checkbox[]" id="checkbox[]"  value='$delID '>SN: ',  $delID' - '$rec['bcName']; 
    It will be easiest to View Source for the resultant page to confirm whether the HTML is malformed.

    FWIW Personally, I wouldn't use 'checkbox' as the name of a checkbox - it only leads to confusion.
    Last edited by AndrewGSW; 01-04-2013 at 01:41 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    rgEffects (01-04-2013)

  • #3
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    Thanks for that hint... I found the problem, and silly me it was right in front of me. I was putting the wrong value in the $delID = $rec['bcSN'];

    instead of calling the serial number from the id of the bizCats I should have been calling the id from the userBizCats table. After changing the Select to userBizCats.id AS delID and rewriting the variable everything was just about fine. I then noticed that I didn't forgot the quotes when echoing the check box value= part of the form. Fixed those lines and everything is now great:

    Repaired code:
    PHP Code:
    <?php
    $parentid 
    0// assuming that 0 is the main category.
    get_sub_catsBC($parentid);
    function 
    get_sub_catsBC($parentid) {

    $sql "SELECT
    bizCats.id AS bcSN, bizCats.bcID, bizCats.bcName,
    userBizCats.bizCatID, userBizCats.id AS delID,
    users.id 
    FROM bizCats, userBizCats, users
    WHERE bizCats.bcID = "
    .$parentid."
    AND userBizCats.bizCatID = bizCats.id
    AND users.id = '35'
    "

    $result mysql_query($sql);
    echo 
    '<ul class="aqtree3clickable">';
        if (
    mysql_num_rows($result) > 0) {
            while (
    $rec mysql_fetch_assoc($result)) {
                
    $delID $rec['delID'];
                echo 
    '<input type="checkbox" name="userChkBox[]" id="userChkBox[]"  value="'$delID,'">SN: ' ,$rec['bcSN'], ' - '$rec['bcName']; 
        
    get_sub_catsBC($rec['bcSN']);
            }
        }
    echo 
    '</ul>';
    echo 
    '</form>';
    }
    ?>
    In case you are curious the users.id set to 35 is set to a testing user ID. On the site the user ID is pulled from record ID query for the user being edited.

    Thanks to all that took a look. I hope this code snippet helps someone in the future.

    Now all I have to do is to figure out how to select the entire family lineage of a child. That's a subject for another post.
    Last edited by rgEffects; 01-04-2013 at 01:51 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •