'; echo '

Texbox value in popup remains zero

I have a php page that contains this block of code:

echo '<div id="popup" style="display:none">'; echo '<form id="AddForm" name="AddForm" method="get">'; echo '<table><tr>'; echo '<td>Software Name: </td><td><input type="text" id="SoftwareName"/></td></tr>'; echo '<tr><td>Software Type:</td><td><input type="text" id="SoftwareType"/></td></tr>'; echo '<tr><td>License Method:</td><td><input type="text" id="LicenseMethod"/></td></tr>'; echo '<tr><td><input type="button" value="Add" OnClick="opener.GetAddData();"></td><td></td>'; echo '</tr></table>'; echo '</form>'; echo '</div>'; 

Buttan, which calls CreatePopup () :

 echo "<input type='submit' value='Add' OnClick='CreatePopup();'/>"; 

I open this div as a popup using the following code:

 function CreatePopup() { var w = null; w = window.open('index.php?List=SoftwareLicenseAllocations', 'test', 'height=125,width=300'); w.document.write( $("#popup").html()); w.document.close(); } 

Code that gets text field values ​​from a popup:

 function GetAddData() { var SoftwareName = document.getElementById('SoftwareName').value;//.getElementById('SoftwareName').value; var SoftwareType = document.getElementById('SoftwareType').value; var LicenseMethod =document.getElementById('LicenseMethod').value; alert(SoftwareName, SoftwareType, LicenseMethod); AddNew(SoftwareName,SoftwareType,LicenseMethod); } 

Screenshot:

Popup

Whenever I call GetAddData () and paste the text into the popup and press the button, the values ​​remain empty.

Why is this happening? How to get text box values?

I am using Pear PHP and a modified version of OpenIT (and the old CMS asset management).

+6
source share
2 answers

If I understand what you're trying to do right ... Maybe this?

Change this line (just added window):

 echo '<tr><td><input type="button" value="Add" OnClick="opener.GetAddData(window);"></td><td></td>'; 

And add the window parameter to the GetAddData function:

 function GetAddData(window) { var popupDoc = window.document; var SoftwareName = popupDoc.getElementById('SoftwareName').value;//.getElementById('SoftwareName').value; var SoftwareType = popupDoc.getElementById('SoftwareType').value; var LicenseMethod = popupDoc.getElementById('LicenseMethod').value; alert(SoftwareName, SoftwareType, LicenseMethod); AddNew(SoftwareName,SoftwareType,LicenseMethod); } 

When you call opener.GetAddData in a popup window, the DOM methods are executed in the opener document, not in the popup window. You need to pass the popup window object into a function so that it knows that it should look for inputs in the document popup.

+1
source

I think your form is submitted using this code.

 echo "<input type='submit' value='Add' OnClick='CreatePopup();'/>"; 

try converting your type 'submit' to 'button'

 echo "<input type='button' value='Add' OnClick='CreatePopup();'/>"; 
0
source

Source: https://habr.com/ru/post/986204/


All Articles