我可以得到另一种形式提交按钮的名称? [英] Can I get the name of submit button in another form?

查看:88
本文介绍了我可以得到另一种形式提交按钮的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有3个提交按钮的形式。它们的名称产生和在一个循环中进行分配。现在,如果我使用POST方法,怎么可以访问提交按钮,被点击的名称。

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 was clicked.

以下是我的code的例子:

The following is the example of my code:

**one.php**

    <form name="one" method="post" action="two.php">

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

    </form>

**two.php**

    {
    code???????
    }

也许我可以用onsubmit属性为one.php按钮标记,但我无法得到输出。有什么建议?

May be I can use onsubmit attribute for the button tag in one.php, but I am unable to get the output. Any suggestions?

推荐答案

您可以只检查 $ _ POST 是否有与每个人的名义进入你的三个按钮:

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:你的,而循环的语法不正确 - 看来你混了的 ,而 和的 ; - )


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

这篇关于我可以得到另一种形式提交按钮的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆