I have a category form.
<tbody>
<?php $no = 0;
foreach ($content as $row) : $no++; ?>
<tr>
<td><?= $no; ?></td>
<td><?= $row->title; ?></td>
<td><?= $row->slug; ?></td>
<td>
<?= form_open("category/delete/$row->categoryid", ['method' => 'POST']) ?>
<?= form_hidden('categoryid', $row->categoryid) ?>
<a href="<?= base_url("category/edit/$row->categoryid") ?>">
<button class="btn btn-sm">
<i class="fas fa-edit text-info"></i>
</button>
</a>
<button class="btn btn-sm"
type="submit"
onclick="return confirm('Are you sure?')">
<i class="fas fa-trash text-danger"></i>
</button>
<?= form_close() ?>
</td>
</tr>
<?php endforeach ?>
</tbody>
When I want to edit one of the items, it trigger delete button instead edit. but when I remove button tag in edit it works fine. please tell me where I went wrong
I have a category form.
<tbody>
<?php $no = 0;
foreach ($content as $row) : $no++; ?>
<tr>
<td><?= $no; ?></td>
<td><?= $row->title; ?></td>
<td><?= $row->slug; ?></td>
<td>
<?= form_open("category/delete/$row->categoryid", ['method' => 'POST']) ?>
<?= form_hidden('categoryid', $row->categoryid) ?>
<a href="<?= base_url("category/edit/$row->categoryid") ?>">
<button class="btn btn-sm">
<i class="fas fa-edit text-info"></i>
</button>
</a>
<button class="btn btn-sm"
type="submit"
onclick="return confirm('Are you sure?')">
<i class="fas fa-trash text-danger"></i>
</button>
<?= form_close() ?>
</td>
</tr>
<?php endforeach ?>
</tbody>
When I want to edit one of the items, it trigger delete button instead edit. but when I remove button tag in edit it works fine. please tell me where I went wrong
Share Improve this question edited Mar 23 at 7:25 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 23 at 5:00 crackerc0decrackerc0de 236 bronze badges 2- Never wrap a button in an A tag. Style the A as a button – mplungjan Commented Mar 23 at 9:07
- okay, thanks for your advice – crackerc0de Commented Mar 23 at 13:44
1 Answer
Reset to default 1The issue happens because you're wrapping the for editing inside an tag. When you click the button, the browser treats it as clicking the tag, and since tags don’t normally submit forms, the event propagates to the nearest with a type="submit"—which is your delete button.
<td>
<?= form_open("category/delete/$row->categoryid", ['method' => 'POST']) ?>
<?= form_hidden('categoryid', $row->categoryid) ?>
<a href="<?= base_url("category/edit/$row->categoryid") ?>" class="btn btn-sm">
<i class="fas fa-edit text-info"></i>
</a>
<button class="btn btn-sm" type="submit" onclick="return confirm('Are you sure?')">
<i class="fas fa-trash text-danger"></i>
</button>
<?= form_close() ?>
</td>