最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Html a tag didn't get what I click - Stack Overflow

programmeradmin4浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

The 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>
发布评论

评论列表(0)

  1. 暂无评论