Two Submit Buttons in One Form

Two submit buttons in one form

If you give each one a name, the clicked one will be sent through as any other input.

<input type="submit" name="button_1" value="Click me">

One form with two submit buttons and different actions for each button

Refer this :

Multiple submit buttons php different actions

Put this script in your page :

function submitForm(action)
document.getElementById('columnarForm').action = action;

Modify your input code :

<input type="image" name="camper" onclick="submitForm('formindb_hoh_1.php')" value="camper" src="../images/apps/camperBtn.png" class="submit_button" />
<input type="image" name="medical" onclick="submitForm('formindb_hoh_2.php')" value="medical" src="../images/apps/medicalBtn.png"class="submit_button" />

Multiple submit buttons in an HTML form

I'm just doing the trick of floating the buttons to the right.

This way the Prev button is left of the Next button, but the Next comes first in the HTML structure:

.f {
float: right;
.clr {
clear: both;
<form action="action" method="get">
<input type="text" name="abc">
<div id="buttons">
<input type="submit" class="f" name="next" value="Next">
<input type="submit" class="f" name="prev" value="Prev">
<div class="clr"></div><!-- This div prevents later elements from floating with the buttons. Keeps them 'inside' div#buttons -->

use many submit buttons in the same form

It won't work the way you've written it. Only the submit button you send will be included in request.form, you'll get an error if you try to use the name of one of the other buttons.

Also, request.form.get is a function, not a dictionary. You can use request.form.get("Histogram") -- this will return the value of the Histogram button if it was used, otherwise it will return None.

Instead of giving the buttons different names, use the same name but different values.

<form id="package_form" action="" method="post">
<div class="panel-body">
<input type ="submit" name="action" value="Download">
<div class="panel-body">
<input type ="submit" name="action" value="Histogram">
<div class="panel-body">
<input type ="submit" name="action" value="Search">


Then your Python code can be:

if request.form['action'] == 'Download':
elif request.form['action'] == 'Histogram':
elif request.form['action'] == 'Search':
... // Report bad parameter

HTML form with two submit buttons and two target attributes

It is more appropriate to approach this problem with the mentality that a form will have a default action tied to one submit button, and then an alternative action bound to a plain button. The difference here is that whichever one goes under the submit will be the one used when a user submits the form by pressing enter, while the other one will only be fired when a user explicitly clicks on the button.

Anyhow, with that in mind, this should do it:

<form id='myform' action='jquery.php' method='GET'>
<input type='submit' id='btn1' value='Normal Submit'>
<input type='button' id='btn2' value='New Window'>

With this javascript:

var form = document.getElementById('myform');
form.onsubmit = function() { = '_self';

document.getElementById('btn2').onclick = function() { = '_blank';

Approaches that bind code to the submit button's click event will not work on IE.

Form with multiple submit buttons or multiple forms

You don't need a separate form for each article, you don't need a hidden input, and you don't need JavaScript. Just use a button instead of an input like the other answer suggested. Any of those buttons will submit the form if they're clicked, and $_POST['id'] will have the value of the button that was clicked.

<button> is different than <input> because it's not a self-closing tag. With an <input>, the value is the button text. But with a <button>, you can give it a value, and then put the text you want it to have between the tags.

Here's an example based on your code.

<form method="POST" action="{{url('/deleteArticle')}}">
{{ csrf_field() }}
@foreach($articles as $a)
<div class="test">
<div class="name"><?= $a['name_a'] ?></div>
<button type="submit" class="del cross" name="id" value='<?= $a['id_a'] ?>' >X</button>

Unrelated to the question, I also fixed the repeated csrf_field and merged the two classes on the button.

Related Topics

Leave a reply