How to add a button value in the ajax request?

Good day, there is code that passes values from the form also has two buttons, one passes the value True, the other false, how to pass advanced this value in ajax request
here is the code
the <script>
 $( document ).ready(function() {
$("button").click(function(){
 sendAjaxForm('ajax_form', btn);
 return false;
}
);
});
 sendAjaxForm function(ajax_form, btn) {

$.ajax({
 url: '/getvote',
 type: "POST",
 dataType: "json",
data:
$("#"+ajax_form).serialize(),

 headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
 success: function (data) {

},
 error: function (er) {

}
});
}
 </script>

The value from the form is transferred out of the button - no .
April 19th 20 at 12:32
1 answer
April 19th 20 at 12:34
Solution
Add a hidden field to the form. When you click on button set this value to true or false. Therefore, when you submit the form sendAjaxForm your hidden field will also be sent.
<button id="1" class="btn btn_primary w-100" value="true">
Pass
</button>
</div>
 <div class="col">
 <button id="0" class="btn btn_danger w-100" value="false">
 Will not work
 </button>

Here buttons below form with indoor input
<input type="hidden" name="vote" value="">
how to pass the value in the value? - Rahul_Jones commented on April 19th 20 at 12:37
The buttons add the class 'vote-btn' , id field='vote'
$('body').on('click','.vote-btn', function(){
$('#vote').val($(this).val());
});
- Jasper_Kunze commented on April 19th 20 at 12:40
@Jasper_Kunzewhat sprite is insert? not working..... - Rahul_Jones commented on April 19th 20 at 12:43
But if inside
$( document ).ready(function() {
? - Jasper_Kunze commented on April 19th 20 at 12:46
$( document ).ready(function() {
 $('body').on('click','.vote-btn', function(){
$('#vote').val($(this).val());
});
 $("button").click(function () {

sendAjaxForm('ajax_form');
 return false;
}
);

 sendAjaxForm function(ajax_form) {

$.ajax({
 url: '/forecast/getvote', //page url (action_ajax_form.php)
 type: "POST", //submit method
 dataType: "json", //data format
data:
 $("#" + ajax_form).serialize(),

 headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
 success: function (data) {

},
 error: function (er) {

}
});
}
 });

so, no need to data transfer - Rahul_Jones commented on April 19th 20 at 12:49
@Rahul_Jones
you
<input type="hidden" id="vote" name="vote" value="">
in #ajax_form added ? What do you have in console? Why didn't codepen if you want to help ?

However, you know better .Pass data! - Jasper_Kunze commented on April 19th 20 at 12:52
- Rahul_Jones commented on April 19th 20 at 12:55
- Jasper_Kunze commented on April 19th 20 at 12:58
@Jasper_Kunze, Thank You. - Rahul_Jones commented on April 19th 20 at 13:01
@Rahul_Jones, good luck. Use Google for simple things. Pump and knowledge of the language and search skills, which are no less important. - Jasper_Kunze commented on April 19th 20 at 13:04

Find more questions by tags jQueryJavaScriptAJAX