Using Checkboxes in Forms

I got most of the information from the site A Brief Review of HTML Forms. I also found the information from the book on this general topic to be most insightful. Previous to doing this weeks assignment I knew how forms worked and separately how checkboxes work but I had never really used them together. I wanted to make a sight that allowed multiple checkboxes to be checked and then get those from a POST.

Function

In building a table in one of my pages I needed the functionality that I described above. From searching the book and the internet I put together some code that gives me exactly what I wanted. I divided it up into two parts. First having a checkbox with each row of the table with the name of the primary key of the row. Second getting the values out to be used to work with the database.

  • echo '<td > <input name="checkbox[]" type="checkbox" value=" '.$num.' "/> </td>'
  • $deletes = $_POST['checkbox'];
    foreach($deletes as $del)

In the first section of code, if the box is checked, then it gets added to the checkbox array. I used the value attribute to store the primary valus of the entry. In the second block, the values from the array in the POST are assigned to $deletes and then the foreach function is used to step through all of the entries. In this way the code will work the same if one or one hundred boxes are checked.

Working Example / References

This is a hard one to show a working example of on the web. As far as references I did find that there are many out there. One really good one that goes really in depth is A Brief Review of HTML Forms. This site it a great reference because it gives multiple examples of all the different types in inputs you can use. There are so many ways to use these commands that it is nice to have a reference that gives general ideas help point you in the right direction.