Jump to content

Close popup after form submitted


Vel

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.