Can I get the name of submit button in another form?

后端 未结 2 1580
孤街浪徒
孤街浪徒 2020-12-20 07:44

I have a form which has 3 submit buttons. Their names are generated and assigned in a loop. Now if I use a post method, how can access the name of the submit button which wa

相关标签:
2条回答
  • 2020-12-20 08:03

    You could just check in $_POST if there is an entry with the name of each one of your three buttons :

    for ($i=1 ; $i<=3 ; $i++) {
        if (isset($_POST[$i])) {
            // here, you are on the clicked button
        }
    }
    


    Note that I'd suggest you give better names (that don't begin with a number) to your buttons -- which means generating your form like this :

    <?php for ($i=1 ; $i<=3 ; $i++) { ?>
    <button type="submit" name="button_<?php echo $i ?>" value="<?php echo $i ?>" >
    </button>
    <?php } ?>
    

    And, on form's submission, using something like this :

    for ($i=1 ; $i<=3 ; $i++) {
        if (isset($_POST['button_' . $i])) {
            // here, you are on the clicked button
        }
    }
    


    BTW: your while loop's syntax is incorrect -- it seems you've mixed up while and for ;-)

    0 讨论(0)
  • 2020-12-20 08:15

    Generate another input element

    <input type="hidden" name="buttonId" value="<?php echo $i ?>" />
    

    And then get your id with $_REQUEST['buttonId']

    0 讨论(0)
提交回复
热议问题