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>
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">
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.
multiple submit buttons with different names one action php
You can on submit check for each individual name... so:
if(isset($_POST['2017-03-02'])){
}
But a better way to do this would be dynamically build radio buttons instead with ONE name, dynamic values. THEN have ONE submit button. Like so:
?php
echo "<form method='post' action=''><table class='turflist'>
<tr>
<th>Turf list Date</th>
</tr>";
foreach($backup_results as $row){
echo "<tr>";
echo "<td><input type='radio' name='dates' class='turfButton' value='". $row['date'] ."'>" . $row['date'] . "/></td>";
echo "</tr>";
}
echo "</table>
echo "<input type='submit' name='submit' value='submit' />";
</form>";
if(isset($_POST['submit'])){
$date = $_POST['dates'];
// RUN YOUR MYSQL QUERY HERE USING $date, which would be the selected radio button
}
?>
How do I use two submit buttons, and differentiate between which one was used to submit the form?
Give each input
a name
attribute. Only the clicked input
's name
attribute will be sent to the server.
<input type="submit" name="publish" value="Publish">
<input type="submit" name="save" value="Save">
And then
<?php
if (isset($_POST['publish'])) {
# Publish-button was clicked
}
elseif (isset($_POST['save'])) {
# Save-button was clicked
}
?>
Edit: Changed value
attributes to alt
. Not sure this is the best approach for image buttons though, any particular reason you don't want to use input[type=image]
?
Edit: Since this keeps getting upvotes I went ahead and changed the weird alt
/value
code to real submit inputs. I believe the original question asked for some sort of image buttons but there are so much better ways to achieve that nowadays instead of using input[type=image]
.
One form 2 submit buttons 2 actions
I changed type to get and now it works.
$body .= "\nFirst Name: ".$_GET['firstName'];
So to have 2 action on 1 form just add second button and post it to php.
Post 2 different form actions with 2 submit buttons
You can use a JS function to redirect to another page for the submit button
Here page.php is where you want to send your form to. formID is the id of the form.
<button class="btn btn-success" type="submit" name="submit_req" value="submit_req" onclick="submitForm('page.php')">Submit Request</button>
<script type="text/javascript">
function submitForm(action)
{
document.getElementById('formID').action = action;
document.getElementById('formID').submit();
}
</script>
Related Topics
Difference Between & and && in PHP
Php's Preg_Match() and Preg_Match_All() Functions
How to Test If a User Has Selected a File to Upload
Codeigniter - Using Multiple Databases
PHP "Pretty Print" HTML (Not Tidy)
How to Remove "Via" and Server Name When Sending Mails with PHP
Fatal Error: Call to Undefined Function Socket_Create()
999 Error Code on Head Request to Linkedin
Load Data Local Infile Forbidden In... PHP
Check If Site Is Inside Iframe
In PHP, What Is the Differences Between Null and Setting a String to Equal 2 Single Quotes
PHP + SQL Server - How to Set Charset for Connection
Get Parent Directory of Running Script
Allowed Memory Size of 262144 Bytes Exhausted (Tried to Allocate 24576 Bytes)