I inherited this project and am trying to correct a few bugs. This particular issue deals with the user double clicking a box in a form(built in conveyor_maint.php), then picking a value in a window that pops up (getPartNumber.php) and then having that value pass back down the form. It successfully pushes the value into the clipboard, but I cannot figure out how to get the value to go back to the form. I would like it to update the form each time the user picks a value in the popup window, but could have it only update upon closure of the popup window too.
Here are the details: The table is in conveyor_maint.php
<th> Override Head Tail </th>
<td><input type="text" name="cd_headtail_ovrd" value="$cd_headtail_ovrd" size="20"
ondblclick="getPartNumber('cd_headtail_ovrd','desc_htail_ovrd','TAIL');"
title="DOUBLE CLICK to display a list of tails you may select from. This will override the calculated head tail when choosing BOM data. YOu may leave this field blank.">
<input type="display" name="desc_htail_ovrd"
value="$desc_htail_ovrd" size="30" readonly></td></tr>
When the block is double clicked, it runs the javascript function getpartNumber, from the included file javascript.php
<script language="javascript">
function getPartNumber(field1, field2, filter) {
var user = '$user_login'
var url = 'getPartNumber.php?user_login=' + user + '&filter=' + filter + '&field1=' + field1 + '&field2=' + field2
window.open(url,'Get_Part_Number','height=700,width=900,scrollbars=yes')
getClipboardContents(field1, field2);
}
async function getClipboardContents(field1, field2) {
try {
const text = await navigator.clipboard.readText();
console.log('Pasted content: ', text);
var partarray = text.split("!")
var partno = partarray[0]
var partdesc = partarray[1]
if (text != "") {
window.document.forms[0].elements[field1].value = partno
window.document.forms[0].elements[field2].value = partdesc
}
} catch (err) {
console.error('Failed to read clipboard contents: ', err);
}
}
</script>
The javascript launches the window.open command to open the pop-up GetPartNumber.php GetPartNumber.php builds a table which the user selects the value to be pushed back to the original form. It relies on a state_change to push the value into the clipboard:
function state_Change() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
myhtml = xmlhttp.responseText;
var dataarray = myhtml.split("~");
var retdata = dataarray[1];
navigator.clipboard.writeText(retdata);
} else {
alert("Problem retrieving XML data:" + xmlhttp.statusText)
}
}
}
This works, as I click the different choices in the popup, they can be pasted into a text file correctly.
Now, this used to work with the following code, but upgrades to the server and related software broke it. Each time a choice is made in the pop-up, it runs SelectPart()
function SelectPart(partno, user_login) {
window.document.curform.partno.value = partno;
var url = 'getPartData.php?partno=' + partno + '&user_login=' + user_login;
loadXMLDoc(url)
}
Subsequently, getPartData.php runs and echoes the same data I pushed to the clipboard. I am trying to get rid of the selectPart() and the subsequent code, unless it turns out to be easier to fix.
The fundamental issue I have is that when the javascript.php runs, it opens the window for the selection and the javascript then completes, reading the clipboard and pushing it to the form (while the popups is still open and the user is making their choice). Subsequent selections, change the clipboard, but they don’t get read until I rerun the popup. I need a way to run the async function which reads the clipboard, either with each selection in the popup, or upon closure of the popup window.
I have tried a variety of things including moving the code to other places, global variables (yuck), and other methods to solve it, so I am hoping for a solution to the original problem.