douke1905
2016-03-02 19:42 阅读 250

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

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 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.
    ?>
    
    点赞 评论 复制链接分享
  • dongshao9106 dongshao9106 2016-03-02 19:50

    Yes you can submit the form using jquery just add a class to your buttons and add a click handler

    $(document).ready(function() {
    
        $( ".buttons_class" ).click(function() {
          $( "#target_form" ).submit();
        });
    });
    

    so your buttons will look like this

      <button type="button" name="activated" class="buttons_class">Activated</button>
      <button type="button" name="pending" class="buttons_class">Pending</button>
      <button type="button" name="suspended" class="buttons_class">Suspended</button>
    

    if using anchors

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

    And in javascript

    $(document).ready(function() {
    
            $( ".buttons_class" ).click(function(e) {
                 e.preventDefault(); //This will stop the default anchor action
           $("#target_form").attr("action", "yourphpfile.php?"+$(this).text()+"=true"); //This will send the text inside the anchor as a GET param. 
              $( "#target_form" ).submit();
            });
        });
    

    However if I were you I would consider using POST instead of GET for this. and do something like this

      $( ".buttons_class" ).click(function(e) {
                     e.preventDefault(); //This will stop the default anchor action
                    var paramName = $(this).text(); //get text inside anchor
                    $( "#target_form" ).submit(function(eventObj) {
                       $('<input />').attr('type', 'hidden')
                         .attr('name', paramName);
                         .attr('value', "something")
                        .appendTo('#form');
                       return true;
                     }); //Add hidden field
               });
    
    点赞 评论 复制链接分享
  • duanpendan8067 duanpendan8067 2016-03-02 20:25

    Change your isset to $_POST instead of $_GET, it will then use the name attributes.

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (isset($_POST['test1'])) {
            ###
        } else if ($_POST['test2']) {
            ###
        }
    }
    
    <form method="post">
    <input name="test1" type="submit" value="TEST 1" />
    <input name="test2" type="submit" value="TEST 2" />
    </form>
    
    点赞 评论 复制链接分享

相关推荐