I'm building a webpage that display a table which has multiple rows and columns. In the beginning of each row I put a check box that allows the user to select, and a button for deleting the selected rows (constructed just like a normal email's inbox). So far I use JavaScript to detect which rows' check boxes have been checked by clicking the delete button. But I couldn't put database related code in those JavaScript functions or pass values of those selected rows from JavaScript's variable to PHP's variable. So I'm stuck at this point. I want to load the same page and see those selected rows are removed from the table after the delete button is clicked. Could someone give me some ideas for solving this problem?

Thanks a lot.

    Your checkboxes can be named according to the rowID of the database, eg checkbox99 (where 99 is the rowID)

    Then run

    for($x=0;$x<$maxrowid;$x++)
    {
        if ($_POST['checkbox'.$x])==1)
        {
            $query=mysql_query("DELETE FROM `table` WHERE rowID='".$x."'");'
        }
    }
    

    Something along those lines might work...

    Or you could delete each row when the delet button is clicked.

    Get Javascript to reload the page, with ?delete=yes&id=xx at the end
    (xx is the row number to delete)

    Then just do the delete SQL as above...

      Another idea is to name the checkboxes as an array

      ie input type='checkbox" name=delete[$your_primary_key])

      and then the sql could be

      delete from table where primary_key in (implode(',',$delete));

      Not exact code, but you should get the picture.

      amc

        Originally posted by amcgrath
        delete from table where primary_key in (implode(',',$delete));
        [/B]

        Never thought of that. It's so simple. I'll have to use something similar next time that comes up.

        One problem, though, is that it seems like you could accidently delete the wrong records if your id's aren't all the same length. Am I wrong? I'm at work, so my brain might not be following this how it should 😛

          No, as long as the values in the array are valid in the database, you should be fine. The only problem you might run into is if the values in the imkplode are character, they would need single quotes...

          amc

            Originally posted by amcgrath
            Another idea is to name the checkboxes as an array

            ie input type='checkbox" name=delete[$your_primary_key])

            and then the sql could be

            delete from table where primary_key in (implode(',',$delete));

            Not exact code, but you should get the picture.

            amc

            Thanks. I implemented the way you told me and its working now. One thing I want to improve is that if no checkbox is checked, after delete button is clicked, it still reload the page. But what I want it to do is if nothing is checked, I don't want to the page reloaded and give the user a warning that checkbox needs to be selected before delete button is clicked. I tried to use JavaScript, but its not working. Could you tell me how to do this?

              I don't really do much Javascript, so I can't offer a solution, though I am sure that there is one out there. Keep looking, you'll find it.

              amc

                Write a Reply...