2

I have a really basic Date system on a site which displays the dates from the Database using a While loop, next to each date that is listed is a check box.

I would like the user to be able to select as many dates as they would like and click submit to remove these dates or edit these dates.

I am having trouble figuring out how I would get the data from multiple check-boxes using only one within the while loop which could be used multiple times?

Here is my current code:

<form action="" method="post">
   <b>Current Dates:</b> <button type="submit" id="remove_dates" name="remove_dates" class="btn btn-danger btn-sm">Remove Selected</button> 
   <hr>
   <div style="overflow-y: scroll;height:100px">
      <?
         $sqldate = "SELECT * FROM `foiu51r_calendar` ORDER BY date DESC";
         $resultdate = $conn->query($sqldate);

         if ($resultdate->num_rows > 0) {
         // output data of each row
         while($rowdate = $resultdate->fetch_assoc()) {
         $date_id = $rowdate['date_id'];
         $dates = $rowdate['date'];

         ?>
      <input type="checkbox" id="date_id" name="date_id" value="<? echo $date_id; ?>"> <b><? echo date("d/m/Y", strtotime($dates)); ?></b><br>
      <?
         }
         }
         ?>
   </div>
</form>
if(isset($_POST['remove_dates'])){
   $date_id = $_POST['date_id'];
}
myselfmiqdad
  • 2,518
  • 2
  • 18
  • 33
Snappysites
  • 804
  • 1
  • 10
  • 41

2 Answers2

2

I believe if you add this: [], to the name attribute, like this: <input type="checkbox" id="date_id" name="date_id[]" value="<? echo $date_id; ?>">. Then it will be handled as an array, which you can loop through with a foreach loop. and insert it into a database (or whatever you like).

Jonas Hoffmann
  • 305
  • 1
  • 9
0

Declare the name as an array, like as follows:

<input type="checkbox" id="date_id" name="date_id[]" value="<? echo $date_id; ?>">.

On submitting it will be handled as an array, Then, you can loop the values of an array and for each element of an array use the id to remove the dates.

xKobalt
  • 1,498
  • 2
  • 13
  • 19