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 :
<script>
function submitForm(action)
{
document.getElementById('columnarForm').action = action;
document.getElementById('columnarForm').submit();
}
</script>
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" />
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>
</div>
@endforeach
</form>
Unrelated to the question, I also fixed the repeated csrf_field and merged the two classes on the button.
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>
<div class="panel-body">
<input type ="submit" name="action" value="Histogram">
</div>
<div class="panel-body">
<input type ="submit" name="action" value="Search">
</div>
</form>
Then your Python code can be:
if request.form['action'] == 'Download':
...
elif request.form['action'] == 'Histogram':
...
elif request.form['action'] == 'Search':
...
else:
... // Report bad parameter
Two submit buttons in one form with different actions
It is fairly common to have multiple actions associated with a single form. The more flexible approach is to not use the native submit type button and instead handle both actions with AJAX requests. For example:
function callAction(action, callback) {
$.ajax({
url: '/path/to/submit/to',
data: {
action: action,
form: $('#update_page_frm').serialize()
},
success: function(response) {
callback(response);
}
});
};
$('#update_page').on('click', function(e) {
e.preventDefault();
callAction('update', function(response) {
console.log('All done!');
});
});
$('#delete_page').on('click', function(e) {
e.preventDefault();
callAction('delete', function(response) {
console.log('All done!');
});
});
Note that the above code was written on the fly here on SO and is untested. Should be fairly close though.
Two submit button in one form for same POST method/action
You can have multiple submit buttons but you would need to involve some javascript to tweak the functionality
You can set your form to be submitted normally with the first submit button but when the second one is licked you need to submit it to a different url. In such case you would need:
$("#secondSubmitButton").click(function(e){
// let's stop default action first e.preventDefault(); $("#yourForm").attr('action', 'http://yournewsubmiterl.com/somelink.php').submit();
});
Multiple submit buttons php different actions
You could add an onclick
method to the new submit button that will change the action of the form and then submit it.
<script type="text/javascript">
function submitForm(action) {
var form = document.getElementById('form1');
form.action = action;
form.submit();
}
</script>
...
<form id="form1">
<!-- ... -->
<input type="button" onclick="submitForm('page1.php')" value="submit 1" />
<input type="button" onclick="submitForm('page2.php')" value="submit 2" />
</form>
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'>
</form>
With this javascript:
var form = document.getElementById('myform');
form.onsubmit = function() {
form.target = '_self';
};
document.getElementById('btn2').onclick = function() {
form.target = '_blank';
form.submit();
}
Approaches that bind code to the submit button's click event will not work on IE.
Related Topics
In PHP, How to Call a Function Interpolated in String
How to Change the Array Key to Start from 1 Instead of 0
How to Cause a Redirect to Occur Before PHP Script Finishes
Php/Gd - Transparent Background
How to Use PHP In_Array with Associative Array
Refresh a Part of Webpage in PHP
PHP Call Class Method/Function
How to Determine Whether It's a Mobile Device with PHP
Change the PHP Path to Mamps PHP
PHP Parsing Problem - &Nbsp; and Â
Sending Data Using Post in Python to PHP
If You Create a Variable Inside a If Statement Is It Available Outside the If Statement
Aescrypt Decryption Between iOS and PHP