Two submit buttons in one form

2018-12-31 05:29发布

I have two submit buttons in a form. How do I determine which one was hit serverside?

18条回答
荒废的爱情
2楼-- · 2018-12-31 05:50
<form method="post">
<input type="hidden" name="id" value="'.$id.'" readonly="readonly"/>'; //any value to post PHP
<input type='submit' name='update' value='update' formAction='updateCars.php'/>
<input type='submit' name='delete' value='delete' formAction='sqlDelete.php'/>
</form>
查看更多
像晚风撩人
3楼-- · 2018-12-31 05:50

I think you should be able to read the name/value in your GET array. I think that the button that wasn't clicked wont appear in that list.

查看更多
余生无你
4楼-- · 2018-12-31 05:54

This is extremely easy to test

<form action="" method="get">

<input type="submit" name="sb" value="One">
<input type="submit" name="sb" value="Two">
<input type="submit" name="sb" value="Three">

</form>

Just put that in an HTML page, click the buttons, and look at the URL

查看更多
一个人的天荒地老
5楼-- · 2018-12-31 05:55

The best way to deal with multiple submit button is using switch case in server script

<form action="demo_form.php" method="get">

Choose your favorite subject:

<button name="subject" type="submit" value="html">HTML</button>
<button name="subject" type="submit" value="css">CSS</button>
<button name="subject" type="submit" value="javascript">Java Script</button>
<button name="subject" type="submit" value="jquery">jQuery</button>

</form>

server code/server script - where you are submitting the form:

demo_form.php

<?php

switch($_REQUEST['subject']) {

    case 'html': //action for html here
                break;

    case 'css': //action for css here
                break;

    case 'javascript': //action for javascript here
                        break;

    case 'jquery': //action for jquery here
                    break;
}

?>

Source: W3Schools.com

查看更多
与君花间醉酒
6楼-- · 2018-12-31 05:57

Simple you can change the action of form on different submit buttons Click.

Try this in document.Ready

$(".acceptOffer").click(function () {
       $("form").attr("action", "/Managers/SubdomainTransactions");
});

$(".declineOffer").click(function () {
       $("form").attr("action", "/Sales/SubdomainTransactions");
});
查看更多
姐姐魅力值爆表
7楼-- · 2018-12-31 05:57

You can also use a href attribute and send a get with the value appended for each button. But the form wouldn't be required then

href="/SubmitForm?action=delete"
href="/SubmitForm?action=save"
查看更多
登录 后发表回答