I’ve been analizing, refactoring and correcting a piece of old JS code (it was written like 15 to 20yrs ago and it is inside a php file containing some older, but still functioning, logic). Now, I have already made some changes to the code to bring up to certain office-needed standards, but there is a function that only works for previously existing pieces of data and does not work if I try to add new data.
To be more clear, this function is supposed to save the changes made to a td when date, text and qty are added to said td. When I edit previously existing data, the code works, but only shows the changes at the next page reload, while when I try to add new data, it doesn’t work at all: it shows the changes on save, but when the page is reloaded, the data is not saved and, hence, not shown.
I need it to, obviously, always save the data and also show the updated td without having to reload the page.
This is the full function:
function savePoConf(id, matCode) {
let pNum = jsfindObj('poNum');
let pPos = jsfindObj('poPos');
let cnfId = jsfindObj('poConfId');
let cnfDate = jsfindObj('poConfDate');
let cnfQty = jsfindObj('poConfQty');
let cnfTxt = jsfindObj('poConfTxt');
const params = new URLSearchParams({
sapConn: '<?php echo $c->sapConn ?>',
poNum: pNum.value,
poPos: pPos.value,
confId: cnfId.value,
confDate: cnfDate.value,
confQty: cnfQty.value,
confTxt: cnfTxt.value,
confUser: '<?php echo CurrentUser() ?>',
sid: Math.random(),
});
// AJAX call
let url = 'https://intranet.intranet.it/api/SAP/intranetFunc.php?' + params.toString();
console.log(url);
let richiestaHTTP = new XMLHttpRequest();
richiestaHTTP.open('GET', url);
richiestaHTTP.send();
richiestaHTTP.onload = function () {
if (richiestaHTTP.status === 200) {
wellDoneHere();
console.log(richiestaHTTP);
try {
let results = JSON.parse(richiestaHTTP.responseText);
if (results.text !== 'ok') {
alert(richiestaHTTP.result + ': ' + richiestaHTTP.responseText);
} else {
$("#poConf").modal('hide');
let addBtn = `<img src="/images/edit_add_16.png" onclick="showPoConf('${matCode}', '${pNum.value}', '${pPos.value}', '${cnfDate.value}', '${cnfQty.value}', '${cnfTxt.value}', '${cnfId.value}')" onmouseover="setAsPointer(this)">`;
let editBtn = `<img src='/images/pgm_edit_16.png' onclick="showPoConf('${matCode}', '${pNum.value}', '${pPos.value}', '${cnfDate.value}', '${cnfQty.value}', '${cnfTxt.value}', '${cnfId.value}')" onmouseover='setAsPointer(this)'>`;
let delBtn = `<img src='/images/delete_16.png' onclick="delPoConf('${pNum.value}', '${pPos.value}', '${cnfId.value}', '${id}')" onmouseover='setAsPointer(this)'>`;
let td = document.getElementById('ConfirmAuth_' + id + '_');
console.log(td);
if (td && td.nextSibling === null) {
let confirmRow = document.createElement('tr');
let confirmRef = document.createElement('td');
confirmRef.setAttribute('id', 'ConfirmRef_' + id + '_');
confirmRef.innerHTML = cnfTxt.value;
let confirmQty = document.createElement('td');
confirmQty.setAttribute('id', 'ConfirmQty_' + id + '_');
confirmQty.innerHTML = cnfQty.value;
let confirmDate = document.createElement('td');
confirmDate.setAttribute('id', 'ConfirmDate_' + id + '_');
confirmDate.innerHTML = cnfDate.value;
let editButton = document.createElement('td');
editButton.innerHTML = editBtn;
let deleteButton = document.createElement('td');
deleteButton.innerHTML = delBtn;
confirmRow.appendChild(confirmDate);
confirmRow.appendChild(confirmQty);
confirmRow.appendChild(confirmRef);
confirmRow.appendChild(editButton);
confirmRow.appendChild(deleteButton);
td.appendChild(confirmRow);
} else {
let newTd = document.createElement('td');
newTd.appendChild(td);
let existingConfAuth = document.createElement('ConfirmAuth_' + id + '_');
let existingConfDate = document.getElementById('ConfirmDate_' + id + '_');
let existingConfQty = document.getElementById('ConfirmQty_' + id + '_');
let existingConfRef = document.getElementById('ConfirmRef_' + id + '_');
existingConfAuth.appendChild(existingConfDate);
existingConfAuth.appendChild(existingConfQty);
existingConfAuth.appendChild(existingConfRef);
let confirmRow = document.createElement('tr');
let confirmRef = document.createElement('td');
confirmRef.setAttribute('id', 'ConfirmRef_' + id + '_');
confirmRef.innerHTML = cnfTxt.value;
let confirmQty = document.createElement('td');
confirmQty.setAttribute('id', 'ConfirmQty_' + id + '_');
confirmQty.innerHTML = cnfQty.value;
let confirmDate = document.createElement('td');
confirmDate.setAttribute('id', 'ConfirmDate_' + id + '_');
confirmDate.innerHTML = cnfDate.value;
let editButton = document.createElement('td');
editButton.innerHTML = editBtn;
let deleteButton = document.createElement('td');
deleteButton.innerHTML = delBtn;
confirmRow.appendChild(confirmDate);
confirmRow.appendChild(confirmQty);
confirmRow.appendChild(confirmRef);
confirmRow.appendChild(editButton);
confirmRow.appendChild(deleteButton);
td.appendChild(confirmRow);
}
}
} catch (e) {
console.error(e);
}
}
}
}
Thank you to anyone who may help!