I need help with some code. I have an input form linked to a table. My code adds a preview button to the input form next to the submit button. I want the preview button to (1) save the record from the current form into the table, but do not change the current scene view in any way. (2) link to another Knack scene designated in Knack as a modal popup. The link is supposed to have a query parameter from the input form appended to it. Here is what is happening: (1) the button is created correctly and it works to link to the correct scene (2) the current record is NOT being saved. (3) the URL is constructed with the appended parameter, it flashes briefly on the screen, and is then replaced by the page without the appended parameter. Help would be greatly appreciated. Code follows:
//Test Popup button
$(document).on(‘knack-scene-render.scene_75’, function(event, scene) {
var submitButton = $(‘button[type=“submit”]’);
if (submitButton.length > 0) {
var previewButton = $('<button>', {
text: 'Preview Mapping',
class: 'kn-button is-primary',
style: 'margin-left: 10px; cursor: pointer;',
type: 'button',
click: function(e) {
e.preventDefault();
var trackValue = $('#field_149').val();
if (!trackValue) {
alert('Please enter a Track value');
return;
}
Knack.showSpinner();
var view = Knack.views['view_136'];
var currentModel = view && view.model;
if (currentModel && typeof currentModel.save === 'function') {
currentModel.set('field_149', trackValue);
currentModel.save({}, {
success: function(model, response) {
Knack.hideSpinner();
var previewUrl = 'https://tprf.knack.com/prlp#preview-student-signup?track=' + trackValue;
window.location.href = previewUrl;
},
error: function(model, xhr) {
Knack.hideSpinner();
alert('Failed to save preview: ' + (xhr.statusText || 'Unknown error'));
}
});
} else {
Knack.hideSpinner();
alert('Failed to save preview: Model save function is unavailable.');
}
}
});
submitButton.first().after(previewButton);
} else {
console.error('Submit button not found in scene_75.');
}
});