Brian
Brian

Reputation: 2051

Texbox value in popup remains null

I have a php page which 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 that calls CreatePopup():

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

I am opening this div as a popup by 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 the textbox values from the 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 insert text in the popup box and click the button, the values remain null.

Why is this happening? How can I get the textbox values?

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

Upvotes: 6

Views: 859

Answers (2)

user4698813
user4698813

Reputation:

If I understand what you're trying to do correctly... Perhaps this?

Change this line (just added window):

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

And add a 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 your popup, the DOM methods are searching in the opener document, and not in the popup. You need to pass the popup's window object to the function, so that it knows it should look for the inputs in the popup's document.

Upvotes: 1

Shravan Sharma
Shravan Sharma

Reputation: 989

I think your form is submitting by 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();'/>";

Upvotes: 0

Related Questions