douke1905 2016-03-02 19:42
浏览 328
已采纳

一个表单中的多个“链接”提交按钮

I would like to create a form with multiple submit link buttons. I know it can be done by using and specifying the name of <button> or <input type="button"> something like this:

In HTML:

<form action="" method="get">
  Other form elements here...
  <button type="submit" name="activated">Activated</button>
  <button type="submit" name="pending">Pending</button>
  <button type="submit" name="suspended">Suspended</button>
</form>

In PHP:

<?php
if(isset($_GET["activated"])) {
  Activated codes here...
}
elseif(isset($_GET["pending"])) {
  Pending codes here...
}
elseif(isset($_GET["suspended"])) {
  Suspended codes here...
}
?>

I want the submit buttons to be done by using link, not <button> or <input type="submit"> something like this:

<a href="#">Activated</a>
<a href="#">Pending</a>
<a href="#">Suspended</a>

I heard that it can be done by using JavaScript or JQuery but I don't know how, anyone knows?

Update: What I want to happen is when I clicked the "Activated" link for example, I want only to process the logic under isset($_GET["activated"]).

The reason behind:

The reason why I want to have a submit link buttons instead of normal submit button tags is that, I want to use this bootstrap dropdown button style to change the status of user(s) on table:

and it is based on links, so that's why.

PS: Sorry for bad English, not my native language.

  • 写回答

3条回答 默认 最新

  • doulai2573 2016-03-02 20:31
    关注

    You could use data attributes on your anchors, then load that attribute into a hidden field to check in your PHP code.

    <form action="" method="post">
        <a href="#" class="anchor-btn" data-name="activated">Activated</a>
        <a href="#" class="anchor-btn" data-name="pending">Pending</a>
        <a href="#" class="anchor-btn" data-name="suspended">Suspended</a>
        <input type="hidden" id="actionName" name="actionName" value="" />
    </form>
    
    $('.anchor-btn').click(function(e) {
        e.preventDefault();
        $('#actionName').val($(this).data('name'));
        $('form').submit();
    });
    
    <?php
        if($_POST['actionName'] == "activated") {
            Activated code goes here
        }
        ...etc.
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?