简体   繁体   English

PHP表单带有多个提交按钮

[英]PHP Form with multiple submit buttons

I have a form with Two buttons. 我有一个带有两个按钮的表单。 It's a submit form which is basically there to increase/Decrease an Integer value in a MySQL database depending on which button they click. 这是一个提交表单,基本上可以根据单击的按钮来增加/减少MySQL数据库中的Integer值。

This is a weird scenario, the page requires an ID extension; 这是一个奇怪的情况,页面需要ID扩展名;

appinfo.php?id=12 appinfo.php?ID = 12

Whenever I click one of the buttons it changes the extension to: 每当我单击按钮之一时,它将扩展名更改为:

appinfo.php?VoteYes=Yes appinfo.php?VoteYes =是

Ideally I'd like it so if someone clicks on Yes it will increase a value within an SQL query and if they click no, it will decrease it. 理想情况下,我希望这样,如果有人单击“是”,它将增加SQL查询中的值,如果他们单击“否”,它将减少它。

Here is what I have so far: 这是我到目前为止的内容:

<form action="<?php echo $_SERVER['PHP_SELF'] . "?id=" . $_GET['id']; ?>"/>
    <input id="submit" name="VoteYes" type="submit" value="Yes" class="btn btn-success"></input>
    <input id="submit" name="VoteNo" type="submit" value="No" class="btn btn-warning"></input>
</form>

In your <form> add the attribute method="post" . 在您的<form>添加属性method="post" The default is GET which is changing your URL. 默认值为GET ,它正在更改您的URL。

So, it should be: 因此,应为:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'] . "?id=" . $_GET['id']; ?>"/>
    <input id="submit" name="VoteYes" type="submit" value="Yes" class="btn btn-success"></input>
    <input id="submit" name="VoteNo" type="submit" value="No" class="btn btn-warning"></input>
</form>

Then in your PHP code, check for $_POST instead of $_GET when checking for the submitted values. 然后在您的PHP代码中,在检查提交的值时检查$_POST而不是$_GET

Hope it helps! 希望能帮助到你!

If you really want the query string You can do this with hidden input that will include the id, I also modified the names to make it less complicated 如果您确实想要查询字符串,则可以使用包含id的隐藏输入来执行此操作,我还修改了名称以使其不那么复杂

<form>
<input id="submit" name="Vote" type="submit" value="Yes" class="btn btn-success" />
<input id="submit" name="Vote" type="submit" value="No" class="btn btn-warning" />
<input  name="id" type="hidden" value="<?php echo $_GET['id']?>" /></form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM