I currently have an html page which contains this part of code:
<td><input type="checkbox" onchange="getButtonDetails(this)" data-toggle="switch" data-title="printing" data-order="{{ $order->order_number }}" class="ct-primary" value="0" /></td>
<td><input type="checkbox" onchange="getButtonDetails(this)" data-toggle="switch" data-title="engraving1" data-order="{{ $order->order_number }}" class="ct-primary" value="0" /></td>
<td><input type="checkbox" onchange="getButtonDetails(this)" data-toggle="switch" data-title="engraving2" data-order="{{ $order->order_number }}" class="ct-primary" value="0" /></td>
<td><input type="checkbox" onchange="getButtonDetails(this)" data-toggle="switch" data-title="assembly" data-order="{{ $order->order_number }}" class="ct-primary" value="0" /></td>
<td><input type="checkbox" onchange="getButtonDetails(this)" data-toggle="switch" data-title="completed" data-order="{{ $order->order_number }}" class="ct-primary" value="0" /></td>
This html above is in a for each loop so the html actually displays about 50 times with a different order number.
I'm trying to see when a person switches the data-toggle switch and then record that info to a database via ajax. My code (based upon newbie knowledge) to accomplish this is as follows:
function getButtonDetails(elem){
var $selfSelection = $(elem);
console.log($selfSelection.data('order'));
if($selfSelection.val() == '1') {
$selfSelection.val('0');
} else {
$selfSelection.val('1');
};
$.ajax({
type: 'POST',
url: 'orders',
data: {
'_token': $('input[name=_token]').val(),
'order_id': $selfSelection.data('order'),
'printing': $selfSelection.filter('[data-title="printing"]').val(),
'engraving1': $selfSelection.filter('[data-title="engraving1"]').val(),
'engraving2': $selfSelection.filter('[data-title="engraving2"]').val()
}
});
toastr.success('Successfully saved', 'Success Alert', {timeOut: 5000});
};
using javascript I'm doing an onchnage event which is calling the function getButtonDetails. The parameter being passed is THIS. Since I'm assuming that'll get THIS input data.
In the function I set var $selfSelection to = $ elem so in essence I'm trying to do $selfSelection = this current element.
I check with a console log ot make sure this indeed is the order I want.
Then I do an if else to set the val of the current selected element. If user switches it'll give it the appropriate value. All this works good.
However when I try to pass data I have been lost. My last failed attempt is to pass the data using a filter to select the data-title of the current element. This updates one column on the database but doesn't update all. What I need is this: When switch is activated on "printing" it should get the val of engraving1, engraving 2 etc at the set time and then fill those fields. Currently if I change one it seems to not recognize any other one.
Any ideas? Again my knowledge is subpar so excuse any newbie mistakes.