0

I have a Dynamic table that has a checkbox on each row. When I click on the check box and then click on my delete button I would like the action to go to my php and delete the row that has a marked checkbox.

I have tried the solution here: Delete selected row from table in php but this did not work for me. I keep getting an error stating that " checkbox is an undefined index"

<?php  
            if (isset($_POST['delete'])){
                $Tasknumber_delete = $_POST['checkbox'];

                mysqli_query($link,"Delete from Universe.Task  where TaskNumber = $Tasknumber_delete");
            }
                ?>


<form class="searchBar"   method="post" action=''>
        <input type="submit" action='POST'  class="btn-group" id="delete" name="delete" value="delete" style="background-color: #757575; font-family: HelveticaNeue;font-size: 13px;">
</form

table class="taskTable"  >
    <tbody class="task-tbody">
        <?php while($row1 = mysqli_fetch_array($Table)){ $task123=$row1[2];?>
        <!-- removed onclick= -->
        <tr class = "task-tr"  onclick="myFunction('<?php echo $task123;?>')">
            <td class="task-td"><input type="checkbox"   name="checkbox" value="<?php echo $task123?>"></td>
            <td class="task-td"> <?php if ($row1[0]=='backlog') {$statuscss= 'statusBacklog';} elseif ($row1[0]== 'inprogress') {$statuscss= 'statusInProgress';} else{ $statuscss= 'statusDone';}    echo '<div class="',$statuscss,'">';?><?php echo $row1[0];?></div></td>
            <td class="task-td"> <?php if ($row1[1]=='HIGH') {$statuscss= 'priorityHigh';} elseif ($row1[1]== 'MEDIUM') {$statuscss= 'priorityMedium ';} else{ $statuscss= 'priorityLow';}    echo '<div class="',$statuscss,'">';?>  <?php echo $row1[1];?></div></td>
            <td class="task-td" > <?php echo $task123;?></td>
            <td class="task-description"> <?php echo $row1[3];?></td>
            <td class="task-td"> <?php echo $row1[4];?></td>
            <td class="task-td"> <?php echo $row1[5];?></td>
            <td class="task-td" width="15"> </td>
        </tr>
        <?php }?>
    </tbody>
</table>

I added the code for the delete php, the form that has the delete button and my table where the check box is located.

  • 1
    The checkboxes are outside your form, so they are not posted along with it. – rickdenhaan Apr 13 '19 at 20:44
  • 1
    Also, this code is wide open to SQL injection. All I'd have to do is simulate a form post with a `checkbox` value of `1 OR 1` and *everything* gets deleted. You should change your code to use a prepared statement instead. – rickdenhaan Apr 13 '19 at 20:46
  • What is your TR onclick js function named : myFunction ? – sylvain Apr 13 '19 at 20:52
  • @ sylvain, yes it is called myfunction, @ rickdenhaan, thanks for that tip, didn't think I need a prepared statment since I am using data that the user doesn't type in. The task number is generated in my database but I will put the checkbox inside my form and see what happens. – rosoceissocool Apr 13 '19 at 21:00

1 Answers1

2

Let's put the closed form-tag at the end of your script and name your checkbox : name="checkbox[]"

<form class="searchBar"   method="post" action=''>
  <input type="submit" action='POST'  class="btn-group" id="delete" name="delete" value="delete" style="background-color: #757575; font-family: HelveticaNeue;font-size: 13px;">

  <table class="taskTable"  >
    <tbody class="task-tbody">
        <?php while($row1 = mysqli_fetch_array($Table)){ $task123=$row1[2];?>
        <!-- removed onclick= -->
        <tr class = "task-tr" onclick="myFunction('<?php echo($task123); ?>')">
            <td class="task-td"><input type="checkbox"   name="checkbox[]" value="<?php echo $task123?>"/></td>
            <td class="task-td"> <?php if ($row1[0]=='backlog') {$statuscss= 'statusBacklog';} elseif ($row1[0]== 'inprogress') {$statuscss= 'statusInProgress';} else{ $statuscss= 'statusDone';}    echo '<div class="',$statuscss,'">';?><?php echo $row1[0];?></div></td>
            <td class="task-td"> <?php if ($row1[1]=='HIGH') {$statuscss= 'priorityHigh';} elseif ($row1[1]== 'MEDIUM') {$statuscss= 'priorityMedium ';} else{ $statuscss= 'priorityLow';}    echo '<div class="',$statuscss,'">';?>  <?php echo $row1[1];?></div></td>
            <td class="task-td" > <?php echo $task123;?></td>
            <td class="task-description"> <?php echo $row1[3];?></td>
            <td class="task-td"> <?php echo $row1[4];?></td>
            <td class="task-td"> <?php echo $row1[5];?></td>
            <td class="task-td" width="15"> </td>
        </tr>
        <?php }?>
    </tbody>
  </table>

</form>
sylvain
  • 853
  • 1
  • 7
  • 20
  • that worked, the only thing I did different was I added one more row in my php to convert an Array to string, $Tas = $_POST['checkbox']; $Tasknumber_delete=implode($Tas); – rosoceissocool Apr 13 '19 at 21:07
  • Actually for whatever reason, now I need to first check the check box, then click delete and then check the check box and click delete again. I am not sure why I have to do it twice. – rosoceissocool Apr 13 '19 at 21:09