Send Value of Submit Button When Form Gets Posted

Send value of submit button when form gets posted

The button names are not submit, so the php $_POST['submit'] value is not set. As in isset($_POST['submit']) evaluates to false.

<html>
<form action="" method="post">
<input type="hidden" name="action" value="submit" />
<select name="name">
<option>John</option>
<option>Henry</option>
<select>
<!--
make sure all html elements that have an ID are unique and name the buttons submit
-->
<input id="tea-submit" type="submit" name="submit" value="Tea">
<input id="coffee-submit" type="submit" name="submit" value="Coffee">
</form>
</html>

<?php
if (isset($_POST['action'])) {
echo '<br />The ' . $_POST['submit'] . ' submit button was pressed<br />';
}
?>

get value of clicked submit button

There are many ways to do this, one would be to add an event handler to the buttons, as the click happens before the submit, and then use a class or something to identify which button was clicked inside the submit handler.

That way you'd still catch and prevent submits that wasn't initiated from the buttons.

If you don't need to catch the submit specifically, you could just use an event handler for the buttons instead of the form submit

$(document).on('click', '.save_form button', function() {    $('.save_form button').removeClass('clicked');    $(this).addClass('clicked');});
$(document).on('submit', '.save_form', function(event) { event.preventDefault(); var formID = $(this).attr('id'); var formDetails = $('#' + formID); var fileurl = $(this).attr('action');
var id = $(this).find('button.clicked').prop('id'); alert(id)});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><form id="new_contact" class="save_form" action="kontaktdetails.php?art='.$_POST['art'].'" method="post">  <button class="btn" id="submit1" type="submit" name="save_close">Save and close<i class="fa fa-save-right"></i>  </button>  <button class="btn" id="submit2" type="submit" name="save_next">Save and next one<i class="fa fa-save-right"></i>  </button></form>

SUBMIT button value sent only when clicked

Yes this the correct behavior.
The value of a submit input is only send when activated or clicked, since here you submit the form through the function it's logical.

Check this example:

<form action="edit.php" method="post">
<input type="text" name="name">
<input type="submit" name="action" value="Save">
<input type="submit" name="action" value="Preview">
</form>

This behavior allow multiple submit action in a form.

__

On checkbox click simulate a click on the submit button instead of using submit()

Make the submit button also send an extra value

Easiest solution: add a hidden input field to your form.

<input type="hidden" name="submitted" value="yes">

HTML form submit POSTS input button/submit values; JavaScript form submit does not

Form 1: Submits text and button
.. default behavior, using a input type submit
Both input controls get submitted because you've clicked the submit button.
Add another submit-button. You will see that only the button dispatching the submit is included in the post data.

So.. what's the reason for that: This way you can add two buttongs, e.g. "cancel" and "save" to a form using the same name

Form 2: Submits text
Form 3: Submits text
.. both solutions look exactly the same to me, the input type button is not handled as an "submit input field" here.. you submit using js.
There is no action on the button and that's why it's not included. (Like described above).

Form 4: Submits twice. 1) Submits text 2) Submits text and button
You're using a input type submit like in the Form 1.. so this form gets submitted exactly the same way. But: there is also a onsubmit handler on the form that calls the submit again using js- that's the reason for the second submit.
The handler is called first, because a submit will trigger a page-reload and the script executing the event will not be "present" anymove after the submit.

The other behaviour is just like described for Fomr 2 & 3
.

Just let me know if you need some further explainations.



Related Topics



Leave a reply



Submit