How to Tell Which Button Was Clicked in a PHP Form Submit

How can I tell which button was clicked in a PHP form submit?

With an HTML form like:

<input type="submit" name="btnSubmit" value="Save Changes">
<input type="submit" name="btnDelete" value="Delete">

(using <form method=POST)

The PHP code to use would look like:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Something posted

if (isset($_POST['btnDelete'])) {
// btnDelete
} else {
// Assume btnSubmit
}
}

You should always assume or default to the first submit button to appear in the form HTML source code. In practice, the various browsers reliably send the name/value of a submit button with the post data when:

  1. The user literally clicks the submit button with the mouse or pointing device
  2. Or there is focus on the submit button (they tabbed to it), and then the Enter key is pressed.

Other ways to submit a form exist, and some browsers/versions decide not to send the name/value of any submit buttons in some of these situations. For example, many users submit forms by pressing the Enter key when the cursor/focus is on a text field. Forms can also be submitted via JavaScript, as well as some more obscure methods.

It's important to pay attention to this detail, otherwise you can really frustrate your users when they submit a form, yet "nothing happens" and their data is lost, because your code failed to detect a form submission, because you did not anticipate the fact that the name/value of a submit button may not be sent with the post data.

Also, the above advice should be used for forms with a single submit button too because you should always assume a default submit button.

I'm aware that the Internet is filled with tons of form-handler tutorials, and almost of all them do nothing more than check for the name and value of a submit button. But, they're just plain wrong!

Edit, here's more examples:

3+ button form:

<input type="submit" name="btnSubmit1" value="1">
<input type="submit" name="btnSubmit2" value="2">
<input type="submit" name="btnSubmit3" value="3">

php:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Something posted

if (isset($_POST['btnSubmit3'])) {
// btnSubmit3
} else if (isset($_POST['btnSubmit2'])) {
// btnSubmit2
} else {
// Assume btnSubmit1
}
}

Single button form:

<input type="submit" name="btnSubmit1" value="1">

php:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Something posted

// Assume btnSubmit1
}

Notice that in all cases, you can and should assume the first submit button to appear in the form's html was the button that was clicked, unless you can detect a different button. Only the buttons which appear later in the form should be explicitly tested for.

In other words, the first button is always assumed to be the form submitter, unless you can detect a different button as the submitter.

What about <form method="GET" ?

The reason we can use $_SERVER['REQUEST_METHOD'] === 'POST' to detect a form POST is because POST is deliberate, while GET is not - GET is the default. So, using $_SERVER['REQUEST_METHOD'] === 'GET' would be unreliable, because someone may just be loading the page/url, and not actually submitting a form, as the browser will use GET in either scenario because it is the default request method.

There's a variety of different ways to reliably detect a GET form submission, but a simple and reliable method is to just add <input type=hidden name=submitted value=1> to the form, and then instead of using if ($_SERVER['REQUEST_METHOD'] === 'POST') do if (isset($_GET['submitted'])) to detect form submission. The code detecting which button was pressed stays the same as it was for POST.

Browser Support:

This strategy has excellent browser support and does not rely on any browser specific behavior, nor any newer html5 features. It should work properly with both modern and ancient browsers, even from the early 2000's. Also, the php code logic is easily adapted to other languages because it does not rely on any tricky or php-specific behaviors.

Multiple Submit buttons, how do determine which one was clicked?

Set value for each submit button and check that in php and find which one is clicked

<form method="POST">
<img src="http://www.foo.com/img.png" id="button_1" name="submit_btn" value="1">
<img src="http://www.foo.com/img.png" id="button_2" name="submit_btn" value="2">
<img src="http://www.foo.com/img.png" id="button_3" name="submit_btn" value="3">
<img src="http://www.foo.com/img.png" id="button_4" name="submit_btn" value="4">
...
<img src="http://www.foo.com/img.png" id="button_100" name="submit_btn" value="100">
</form>

echo $_POST['submit_btn']; will give you the value of which submit button is clicked

Determining which Submit button was clicked, when an array of buttons is used

the post data will always include all buttons but only the one which was clicked will have a value. this way you can determine easily which button was clicked by checking which value in the button array is not empty. (The value will be the text on the button as that text is defined in the value attribute).

How can you determine which submit button was pressed, when the name attributes are in an array?

When clicking on a submit-button, only the clicked submit-button are sent with post-request to php (the server). Therefore keep the same name (no array) on the submit-buttons and check which button has been sent by checking the value of the submit-button.

Something like:

<?php
if ($_POST['submitaction'] == 'Delete article') {
//action for delete
}
if ($_POST['submitaction'] == 'Edit Article')
{
//action for edit
}

//Your code
echo "<form action='edit.php' method='post'>";
while ($display = mysqli_fetch_assoc($newArticles)) {
....
echo "<input type='submit' class='col-sm-offset-5
btn btn-default' name='submitaction' value='Delete article'>" .
"<input type='submit' class='col-sm-offset-1 btn
btn-default' name='submitaction' value='Edit Article'>" ."<br/>"
....
}
echo "</form>

How to distinguish which button was clicked in the html form and based on that pass different values with ajax?

Here depends on functionality of validation plugin, when it reacts, but likely you can try to add onclick to buttons which sets some hidden variable, indicating which button was pushed. Like this:

<input type="submit" id="sendfeedback" onclick="this.form.clickedbtn.value=1" value="now" disabled/>

<input type="submit" id="postmelater" value="send" onclick="this.form.clickedbtn.value=2" disabled/>

and also add hidden input to the form like this

<input type="hidden" id="clickedbtn" name="clickedbtn">

Than in the handler add

var clickedbtn = $("#textarea").val();

...

clickedbtn: clickedbtn,

so form will look like this:

<form class="center" id="myform">
<input type="hidden" id="clickedbtn" name="clickedbtn">
<p>
<input id="email" name="email" type="email" class="textox email" title="" placeholder="your@email.com" required>
</p>
<textarea name="slogan" id="textarea" maxlength="140" style="resize:none" class="textoxarea" title="Please enter at least 5 characters" placeholder="Placeholder" ></textarea>
I accept terms


</p>
<input type="submit" id="sendfeedback" value="now" onclick="this.form.clickedbtn.value=1" disabled/>
<input id="datetimepicker" type="text" readonly="readonly">
<input type="submit" onclick="this.form.clickedbtn.value=2" id="postmelater" value="send" disabled/>
</form>

And handler will look like this:

submitHandler: function (form) { // for demo
alert('valid form submitted'); // for demo
var mail = $("#email").val(); //mg
var text = $("#textarea").val();
var date = 0;
var stand = 1;
var clickedbtn = $("#textarea").val();
$.ajax({
url: 'savedatanow.php'
type: "POST",
data: {
mail: mail,
text: text,
date: date,
clickedbtn: clickedbtn,
stand: stand

},
success: function(response)
{

alert(response);
}
});

}

After that in php script you can check

if ($_POST["clickedbtn"]==1) {
send now code
} else {
other code
}

Identifying which button has triggered Form Post

There's nothing to change in your <form /> here, thour PHP script will know which button has been clicked thanks to the name attribute :

<?php
if (isset($_POST['button1'])) {
echo 'button1 has been clicked!';
} else if (isset($_POST['button2'])) {
echo 'button2 has been clicked!';
}

How to determine which button in a single form was clicked

Give them different names and then in PHP check which name is set

HTML

<input type="submit" name="save" value="Save" />
<input type="submit" name="savenew" value="Save & new" />

PHP

if (isset($_POST['save'])) {
// save button
}
elseif(isset($_POST['savenew']))
{
// save & new
}

etc.

how can I check which submit button is clicked

As per my comment, you can set the same name, and a different value for each submit button, then check the value in the controller:

{{Form::submit('send',array('class'=>'btn btn-primary','name'=>'action','value'=>'send'))}}
{{Form::submit('cancel',array('class'=>'btn btn-primary','data-dismiss'=>'modal','aria- hidden'=>'true','name'=>'action','value'=>'cancel'))}}
{{Form::submit('save',array('class'=>'btn btn-primary','name'=>'action','value'=>'save'))}}

//controller:

$action = Input::get('action', 'none');

if($action=='send'){
//do send
}else if($action=='save'){
//do save
}


Related Topics



Leave a reply



Submit