问题
I am trying to post the $GLABALS variable to another php file and get it to open in a popup and load the printOnly.php file with the value of the $GLOBLAS variable (which passes the name of the xml file used by $dom.
Currently it opens the popup but cannot find the value of the variable posted but it also loads the variable into the existing navigator window where the value of the posted variable is retrieved without a problem. I want the main window to stay on the original php page and only load the file called in the form to the popup.
On the original page I have:
<form name ="printView" method ="post" action="printOnly.php" target="popUp" >
<input type="hidden" name="toPopFile" value="'.$GLOBALS["file"].'" />
<input type="image" value="submit" class="button" title="print view" src="graphics/printview.png" align="middle" onclick="javascript:viewClick(\'printOnly.php\')" />
</form>
In the file to be loaded into the popup I have:
$file=basename($_POST['toPopFile']); // value to be retrieved which is the name of the xml file currently loaded in original php file
$dom = new domDocument;
if (file_exists($file)) {
$dom->load($file);
} else {
exit('Error ! xml file not found.');
}
And here is the javascript function being called from an external .js file
var view;
function viewClick(url) {
view= window.open(url,'view text','menubar=yes,scrollbars=yes,resizable=yes,width=640,height=700');
view.focus();
}
Help! (and keep it simple, I can parrot well, but I don't always understand what I'm doing)
回答1:
Your form is using a POST
method already, so I will start with that. It means using your submit
button as a real HTML submit button (ie drop the onclick
event). Then add a onsubmit
handler to your post:
<form name="printView" method ="post" action="printOnly.php" target="popUp" onsubmit="popup(this);">
Add this function in your Javascript to create the popup, and then submit the form to it:
function popup(form) {
window.open('', 'formpopup', 'view text','menubar=yes,scrollbars=yes,resizable=yes,width=640,height=700');
form.target = 'formpopup';
}
来源:https://stackoverflow.com/questions/12041677/load-a-php-file-into-a-popup-and-post-php-variable-to-that-popup-using-onclick