Vel Posted April 7, 2011 Share Posted April 7, 2011 Hi, I have a php form that opens a popup so an admin can select chose a username to edit. The popup contains a form that then passes the selected username back to the parent page. Once the information is passed back to the parent how do I get the parent to close the popup? I think I've tried every combination of newwin.close() that I can think of. Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/ Share on other sites More sharing options...
nogray Posted April 7, 2011 Share Posted April 7, 2011 You should make the pop-up close itself (e.g. window.close()) after sending the value. Parent window cant' close the pop up unless you have specific security settings or a trusted html application. Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1198469 Share on other sites More sharing options...
Vel Posted April 7, 2011 Author Share Posted April 7, 2011 OK, the only problem is I can't us an onclick event for the submit button as it runs a verify script and if the verify script returns false the form isn't submitted and I need the window to stay open. How would I get it to close only if data has been passed back to the parent? Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1198471 Share on other sites More sharing options...
Adam Posted April 8, 2011 Share Posted April 8, 2011 Parent window cant' close the pop up unless you have specific security settings or a trusted html application. Can't it..? <script type="text/javascript"> var myWindow; function openWindow() { myWindow = window.open('child-window.html'); } function closeWindow() { if (typeof(myWindow) != 'undefined') { myWindow.close(); } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1198654 Share on other sites More sharing options...
Vel Posted April 8, 2011 Author Share Posted April 8, 2011 With that I get: Fatal error: Call to undefined function closeWindow() in C:\wamp\www\fearless\admin.php on line 93 I put the javascript code in the header. Do you have to call it in some special way? Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1198664 Share on other sites More sharing options...
Adam Posted April 8, 2011 Share Posted April 8, 2011 That's a PHP error - this is JavaScript code. Can you post the code as you have it from and around line 93 of admin.php? Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1198666 Share on other sites More sharing options...
Vel Posted April 8, 2011 Author Share Posted April 8, 2011 Line 93 is literally: closeWindow(); Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1198668 Share on other sites More sharing options...
Adam Posted April 8, 2011 Share Posted April 8, 2011 Can you post the code as you have it from and around line 93 of admin.php? You need to post the code surrounding it too, so that we get a jist of the context it's used in. Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1198669 Share on other sites More sharing options...
Vel Posted April 8, 2011 Author Share Posted April 8, 2011 Ah, sorry. I have an admin control panel. This part is for finding newly registered users, selecting a user and then dealing with it. The code I have is: case "new": echo "<a href=\"admin.php?mode=0\">Back</a><br>"; //Initiate variables $newuser = NULL; $newuid = NULL; $newapi = NULL; $newmember = 0; $sql = "SELECT * FROM users WHERE newreg='1'"; $result = mysql_query($sql); $count = mysql_num_rows($result); $row = mysql_fetch_array($result); //Check for new accounts if one hasn't already been selected if(!isset($_GET['newuser'])) { if($count == 1) { $newuser = $row['username']; header("location:admin.php?mode=new&newuser=$newuser"); } elseif($count > 1) { echo "There is more than 1 new user:<br>"; echo "<a href=\"select_user.php?mode=new\" onclick=\"return popup('select_user.php?mode=new')\">Select User</a><br>"; //Open popup } else echo "There are no new users.<br>"; } //Else get details for account and display in a form else { closeWindow(); //Close popup - LINE 93 $newuser = $_GET['newuser']; $sql = "SELECT * FROM users WHERE username='$newuser'"; $newuid = $row['uid']; $newapi = $row['api']; $newmember = $row['member']; echo "<form name=\"newApplication\" method=\"post\" action=\"verify_admin.php?form=new\">"; echo "Username: " . $newuser . "<br>"; echo "UID: " . $newuid . "<br>"; echo "API: " . $newapi . "<br>"; echo "Member: "; if($newmember == 0) echo "<input name=\"newmember\" type=\"checkbox\" id=\"newmember\" value=\"1\"><br>"; else echo "<input name=\"newmember\" type=\"checkbox\" id=\"newmember\" checked=\"checked\" value=\"1\"><br>"; echo "<input name=\"newuser\" type=\"hidden\" id=\"newuser\" value=\"" . $newuser . "\">"; echo "<input name=\"submit\" type=\"submit\" value=\"Submit\"><br>"; echo "Note: Upon submission the \"new account\" flag will be removed."; echo "</form>"; } break; Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1198671 Share on other sites More sharing options...
Adam Posted April 8, 2011 Share Posted April 8, 2011 Ah - you're mixing PHP with JavaScript. You have to remember that they execute at a completely different time. The PHP is parsed by the server as the page is requested, then hands back the resulting mark-up to the browser. JavaScript is then parsed by the browser, based on the mark-up that the server/PHP returned, and is run as and when the event handlers are called. So with that in mind, at what point should this pop-up be closed? Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1198673 Share on other sites More sharing options...
Vel Posted April 8, 2011 Author Share Posted April 8, 2011 The popup should be closed as soon as it passes the form back to the parent admin page. This is the popup page: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Fearless Bandits :: Select User</title> </head> <body> <?php //Check for config file and initiate session. require("config.php"); session_start(); //Connect to DB if(!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("$dbname") or die("Cannot select DB"); //Load mode if(isset($_GET['mode'])) $mode = $_GET['mode']; else $mode = NULL; //Select users based on mode switch($mode) { case "new": //Load the names of all new accounts into a form, verify that an account is selected then pass the result back $sql="SELECT * FROM users WHERE newreg='1'"; $result = mysql_query($sql); $count = mysql_num_rows($result); echo "<form name=\"selectUser\" method=\"get\" action=\"admin.php\" target=\"adminCP\" onSubmit=\"return Validate();\">"; echo "<select name=\"newuser\">"; echo "<option value=\"Select User\" selected=\"selected\">Select User</option>"; while($row = mysql_fetch_array($result)) { echo "<option value=\"" . $row['username'] . "\">" . $row['username'] . "</option>"; } echo "</select>"; echo "<input name=\"mode\" type=\"hidden\" id=\"mode\" value=\"new\">"; echo "<input name=\"submit\" type=\"submit\" value=\"Submit\">"; echo "</form>"; break; case "modify": //Load the names of all account into a form, verify that an account is selected then pass the result back $sql="SELECT * FROM users"; $result = mysql_query($sql); $count = mysql_num_rows($result); echo "<form name=\"selectUser\" method=\"get\" action=\"admin.php\" target=\"adminCP\" onSubmit=\"return Validate();\">"; echo "<select name=\"newuser\">"; echo "<option value=\"Select User\" selected=\"selected\">Select User</option>"; while($row = mysql_fetch_array($result)) { echo "<option value=\"" . $row['username'] . "\">" . $row['username'] . "</option>"; } echo "</select>"; echo "<input name=\"mode\" type=\"hidden\" id=\"mode\" value=\"modify\">"; echo "<input name=\"submit\" type=\"submit\" value=\"Submit\">"; echo "</form>"; break; default: echo "ERROR: No mode selected. Please go back and try again. If you see keep getting this error message please "; echo "<a href=\"mailto:admin@fearless-bandits.co.cc\">contact the administrator</a><br>"; echo "<a href=\"JavaScript:window.close()\">Close</a>"; break; } mysql_close($con); ?> <script language = "Javascript"> //Validation of form. Makes sure that a user has been selected. function Validate() { if (document.selectUser.account.value == 'Select User') { alert('Please select a user before submitting.'); return false; } return true; } </script> </body> </html> Everything I've read says I can only close that popup with Javascript, and I can have it close as soon as the page loads, but I just don't know how. If there's a way to do it in php I'd be happy to learn. Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1198675 Share on other sites More sharing options...
Adam Posted April 8, 2011 Share Posted April 8, 2011 Could you describe the process in full please? What fires the pop-up, what happens within the pop-up, and where closing it fits into it / what event triggers the close? I don't really have the time to look through the code and work it out all I'm afraid. Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1198715 Share on other sites More sharing options...
Vel Posted April 8, 2011 Author Share Posted April 8, 2011 That's fine. The code is split over 2 pages, both coded in php. The first page first looks to see if an account has been selected. If not it checks to see if there are any new accounts in the database. If there is more than 1 new account (there is a flag in the database for new accounts) it offers the users a hyperlink, which triggers the second page in the form of a popup. In the popup a form is loaded with a selection box, and in the selection box is loaded all usernames of the new accounts. The default selection is "Select User" and links to no account, just so there is some text with instructions for the user and for verification. The user then selects an account from the drop down list and submits the form. A javascript verification process verifies that the "Select User" option isn't still selected, and then the form is passed back using the get method to the parent window. The admin page is loaded again with the form information available. The admin page detects the new information and then proceeds to fill the selected account into a form. Once the parent window has the information, I need the popup to close. Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1198728 Share on other sites More sharing options...
Vel Posted April 10, 2011 Author Share Posted April 10, 2011 Can anyone suggest how to close this popup please? Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1199628 Share on other sites More sharing options...
Vel Posted April 10, 2011 Author Share Posted April 10, 2011 Solved. The problem was after reloading admin.php it no longer new the name of the popup window. I added an unload event that checked to see if the popup was open, and if so, close it. When the form in the popup passes the information back to the parent window and reloads the admin.php page it triggers the unload event, closing the popup. Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1199740 Share on other sites More sharing options...
Adam Posted April 11, 2011 Share Posted April 11, 2011 Glad you sorted it. I'm not online much at the weekend or I'd have chipped in sooner.. Quote Link to comment https://forums.phpfreaks.com/topic/233020-close-popup-after-form-submitted/#findComment-1199939 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.