This subject has been brought up before I think. I really do appreciate everyone's input, it really helps when I'm troubleshooting! Thanks everyone!
My issue is I'm trying to update a field when a button is clicked. I think my jquery code is correct but I must be doing something wrong in the Ajax code, it keeps telling me that it's a bad request because of invalid syntax. Can anyone help?
My code is below:
var addCheckboxes = function(view) {
// add the checkbox to to the header to select/unselect all
$('#' + view.key + '.kn-table thead tr').prepend('<th><input type="checkbox"></th>');
$('#' + view.key + '.kn-table thead input').change(function() {
$('.' + view.key + '.kn-table tbody tr input').each(function() {
$(this).attr('checked', $('#' + view.key + '.kn-table thead input').attr('checked') != undefined);
});
});
// add a checkbox to each row in the table body
$('#' + view.key + '.kn-table tbody tr').each(function() {
$(this).prepend('<td><input type="checkbox"></td>');
});
}
// on page load
$(document).on('knack-view-render.view_395', function (event, view) {
//add an update button
$('<button id="print">Update Labels</button>').insertAfter('.view-header');
//add checkboxes to table
addCheckboxes(view);
// add the checkbox to to the header to select/unselect all
$('#' + view.key + '.kn-table thead tr').prepend('<th><input type="checkbox"></th>');
$('#' + view.key + '.kn-table thead input').change(function() {
$('.' + view.key + '.kn-table tbody tr input').each(function() {
$(this).attr('checked', $('#' + view.key + '.kn-table thead input').attr('checked') != undefined);
});
});
// add a checkbox to each row in the table body
$('#' + view.key + '.kn-table tbody tr').each(function() {
$(this).prepend('<td><input type="checkbox"></td>');
});
}
// on page load
$(document).on('knack-view-render.view_395', function (event, view) {
//add an update button
$('<button id="print">Update Labels</button>').insertAfter('.view-header');
//add checkboxes to table
addCheckboxes(view);
$(function() {
$('#print').on('click', function() {
var record_ids = [];
//populate the record IDs variable using all the checked rows
$('#' + view.key + 'tbody input[type=checkbox]:checked').each(function() {
record_ids.push($(this).closest('tr').attr('id'));//record id
});//closing for the each function
//turn on the Knack spinner
Knack.showSpinner();
//set the variable we want changed
var data = {
field_298: 'Yes'
};
//set a delay
var myDelay = 100;
$(function() {
updateRecords(record_ids.shift(),record_ids,data)
});
var record_ids = [];
//populate the record IDs variable using all the checked rows
$('#' + view.key + 'tbody input[type=checkbox]:checked').each(function() {
record_ids.push($(this).closest('tr').attr('id'));//record id
});//closing for the each function
//turn on the Knack spinner
Knack.showSpinner();
//set the variable we want changed
var data = {
field_298: 'Yes'
};
//set a delay
var myDelay = 100;
$(function() {
updateRecords(record_ids.shift(),record_ids,data)
});
var selectedRecords = record_ids.length + 1
function updateRecords(id, records, data) {
$.ajax({
url: 'https://api.knackhq.com/v1/objects/object_9/records/',
type: 'PUT',
headers: {'X-Knack-Application-ID':'xxxxx',
'X-Knack-REST-API-Key': 'xxxxxx',
'content-type':'application/json',
},//closing for headers
data: data,
success:function(response){
if(record_ids.length >0){
setTimeout(updateRecords(record_ids.shift(),record_ids,data),myDelay);
}//closing for if function
else {
alert('You have updated' + selectRecords + 'horses');
}//closing for else statement
},//closing for success function
error: function() {
alert('There was an error updating the labels');
Knack.hideSpinner();
}
});
};
});//closing for click function
function updateRecords(id, records, data) {
$.ajax({
url: 'https://api.knackhq.com/v1/objects/object_9/records/',
type: 'PUT',
headers: {'X-Knack-Application-ID':'xxxxx',
'X-Knack-REST-API-Key': 'xxxxxx',
'content-type':'application/json',
},//closing for headers
data: data,
success:function(response){
if(record_ids.length >0){
setTimeout(updateRecords(record_ids.shift(),record_ids,data),myDelay);
}//closing for if function
else {
alert('You have updated' + selectRecords + 'horses');
}//closing for else statement
},//closing for success function
error: function() {
alert('There was an error updating the labels');
Knack.hideSpinner();
}
});
};
});//closing for click function
});
});
Here is what shows up in the console log:
HTTP400: BAD REQUEST - The request could not be processed by the server due to invalid syntax. (XHR)PUT - https://api.knackhq.com/v1/objects/object_9/records/