I have project similar to this example.
<?php
if (isset($_POST['delete'])) {
// Delete using id
}
if (isset($_POST['deleteAll'])) {
// Delete using array name check
}
?>
<form method="POST" action="index.php" onsubmit="return validate()"> // First form
<?php foreach ($rows as $row) : ?>
<form method="POST" action="index.php" onsubmit="return validate()> // Second form
<input type="checkbox" name="check[]" value="<?php $row['id'] ?>" />
<input type="hidden" name="id" value="<?php $row['id'] ?>" />
<input type="submit" name="delete" value="Delete" />
</form>
<?php endforeach ?>
<input type="submit" name="deleteAll" value="Delete checked items" />
</form>
onsubmit
used to use confirm function in javascript
.
I already think about this and only can think to use nested-form
.
second form
and submit delete
used to delete
using ID
.
first form
and submit deleteAll
used to delete
all checked items.
if I delete second form
,
and I click delete
the id
will be the last index
of foreach
.
So I will delete
the last row
, not the row
I wanted to delete
.
I already thinking and found no clue,
I hope someone here can give a clue.
Don't try to do it that way. You're just making it more complicated. A single form should suffice.
<form method="POST" action="index.php" onsubmit="return validate()">
<?php foreach ($rows as $row) : ?>
<input type="checkbox" name="check[]" value="<?php echo $row['id'] ?>" />
<button name="delete" value="<?php echo $row['id']; ?>">Delete</button><br/>
<?php endforeach ?>
<input type="submit" name="deleteAll" value="Delete checked items" />
</form>
PHP
// handle single delete
if(isset($_POST['delete'])) {
$id = $_POST['delete']; // individual ID
}
// handle mulitple selected checkboxes
if(isset($_POST['deleteAll'])) {
$ids = $_POST['check'];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With