Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

Why does this code fire again?

I've got a pop up window executing an insert statement.  I've got a code that refreshes the parent screen when the user closes the pop up window. For some reason though, another insert statement is being fired and I don't understand why.

Here's my header with the insert code and the Javascript function:

<?php
date_default_timezone_set('America/Chicago');

require_once('model_class.php');
$display=new CalendarAdmin();
$addition=$display->calendar_insert();
$display_date = date('F j, Y', strtotime($_POST['sdate']));	
?>

<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Calendar Add Page</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script type="text/javascript"> 
window.onunload = refreshParent;
function refreshParent() {
	window.opener.location.reload();
}
</script>
</head>

Open in new window


The button looks like this:

<input type="image" src="images/close_button.jpg" onclick="window.close()">

Why would my insert statement fire again after my user closes the window? What do I need to change?
Avatar of Russ Suter
Russ Suter

You have a JavaScript function that reloads a page when it unloads? I'm surprised it's not in a perpetual loop.
Avatar of Bruce Gust

ASKER

How do I fix it?
I'd need to see your HTML body to have any idea what you're trying to accomplish. Perhaps you can post a test page somewhere so we can see it in action?
Stand by...
Here you go: http://brucegust.com/portfolio/calendar/

Click on a triangle and add a date. Don't do an event that spans multiple days, just go for a single date. After you do it, you'll get the next screen which invites you to close the window. At that poin, for some reason, the insert statement seems to be firing again. Only this time, it doesn't insert a real date, just part of the info.

Here's my insert page:

<?php
/date_default_timezone_set('America/Chicago');
/*session_start();
if(empty($_SESSION['email']))
{
	header("location:custom_gate.php");
	exit();
}*/

?>

<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title><?php echo $project_name; ?>Calendar Add Page</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script type="text/javascript"> 

 $(function() {
	$("#start_date").datepicker({'onSelect':function(dateText){
		  if( dateText.match(/\d\d\d\d-\d\d\-\d\d/) )
		  {
			   $(this).css({"color":"#000000"});
		   }
		  else
		  {
			   $(this).css({"color":"#CCCCCC"});
		   }
		},		
	dateFormat:"yy-mm-dd"});
});

$(function() {
	$("#end_date").datepicker({'onSelect':function(dateText){
		  if( dateText.match(/\d\d\d\d-\d\d\-\d\d/) )
		  {
			   $(this).css({"color":"#000000"});
		   }
		  else
		  {
			   $(this).css({"color":"#CCCCCC"});
		   }
		},		
	dateFormat:"yy-mm-dd"});
});

</script>
</head>

<body style="background-image:url(images/notes_background.jpg); background-repeat: no-repeat;">
	<div style="width:600px; height:560px; overflow:auto; margin-left:15px;">
	<br><br>To add an event for <b><?php echo date("F jS, Y", strtotime($_GET['the_date'])); ?></b>, fill in the fields below and click on "submit."<br><br>Thanks!<br>
	
	<br><br><table class="page_display" style="width:auto; margin:auto;"><form action="calendar_insert_execute.php" method="post">
	<tr>
		<td>
		short name
		</td>
		<td>
		<input type="text" size="65" name="short_name">
		</td>
	</tr>
	<tr>
		<td style="width:75px;;">start date</td>
		<td>
		<input type="text" size="65" name="sdate" id="start_date" style="border:1px solid #000000; color:#cccccc;" value="<?php echo date("Y-m-d", strtotime($_GET['the_date'])); ?>">
		</td>
	</tr>
	<tr>
		<td>end date</td>
		<td>
		<input type="text" size="65" name="edate" id="end_date" style="border:1px solid #000000; color:#cccccc;" value="end date">
		</td>
	</tr>
	<tr>
		<td>
		state
		</td>
		<td colspan="5">
		<select name="state">
		<option><option>
		<option>Alabama</option>
		<option>Arkansas</option>
		<option>Florida</option>
		<option>Georgia</option>
		<option>Kentucky </option>
		<option>Louisiana</option>
		<option>Mississippi</option>
		<option>North Carolina</option>
		<option>Oklahoma</option>
		<option>Region</option>
		<option>South Carolina</option>
		<option>Tennessee</option>
		<option>Texas</option>
		<option>____________________________________________________</option>
		</select>
		</td>
	</tr>
	<tr>
		<td>
		<a href="images/states.jpg" target="_blank">calendar</a>
		</td>
		<td colspan="5">
		<select name="region">
		<option><option>
		<option value="ALU">Alcatel-Lucent (ALU)</option>
		<option value="AREA">Area /National</option>
		<option value="CATN">Carolinas / Tennessee</option>
		<option value="CTX">Central Texas</option>
		<option value="DEVICE">Device</option>
		<option value="ERC">ERC</option>
		<option value="FL">Florida</option>
		<option value="GAAL">Georgia / Alabama</option>
		<option value="HGC">Houston Gulf Coast </option>
		<option value="SCTL">South Central</option>
		<option>____________________________________________________</option>
		</select>
		</td>
	</tr>
	<tr>
		<td colspan="6"><br>
		<textarea name="description" class="description">Main Body</textarea>
		</td>
	</tr>
</table>
<br><br><div style="text-align:center;"><input type="image" src="images/submit_button.png" alt="Submit" width="100"></div></form>
	</div>

</body>

</html>

Open in new window


...and here's my insert execute page:

<?php
date_default_timezone_set('America/Chicago');

require_once('model_class.php');
$display=new CalendarAdmin();
$addition=$display->calendar_insert();
$display_date = date('F j, Y', strtotime($_POST['sdate']));	
?>

<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Calendar Add Page</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script type="text/javascript"> 
window.onunload = refreshParent;
function refreshParent() {
	window.opener.location.reload();
}
</script>
</head>

<body style="background-image:url(images/notes_background.jpg); background-repeat: no-repeat;">
	<div style="width:600px; height:560px; overflow:auto; margin-left:15px;">
	<br><br>Here's the event you just inserted into the database! Click here to close this window and<br>refresh the calendar page.<br><br>
	<table class="page_display" style="width:auto; margin:auto;"><form action="calendar_insert_execute.php" method="post">
		<tr>
			<td>
			short name
			</td>
			<td>
			<input type="text" size="65" name="short_name" value=<?php echo $_POST['short_name']; ?>>
			</td>
		</tr>
		<tr>
			<td style="width:75px;;">
			start date
			</td>
			<td>
			<input type="text" size="65" name="date" value="<?php echo $display_date; ?>">
			</td>
		</tr>
		<?php
		if(isset($_POST['edate'])&& ($_POST['edate']<>"end date"))
		{
		$display_end_date = date('F j, Y', strtotime($_POST['edate']));
		?>
		<tr>
			<td>
			end date
			</td>
			<td><input type="text" size="65" name="end date" value="<?php echo $display_end_date; ?>"></td>
		</tr>
		<?php
		}
		?>
		<tr>
			<td>
			state
			</td>
			<td>
			<select name="state">
			<option selected><?php echo $_POST['state']; ?><option>
			<option>Alabama</option>
			<option>Arkansas</option>
			<option>Florida</option>
			<option>Georgia</option>
			<option>Kentucky </option>
			<option>Louisiana</option>
			<option>Mississippi</option>
			<option>North Carolina</option>
			<option>Oklahoma</option>
			<option>South Carolina</option>
			<option>Tennessee</option>
			<option>Texas</option>
			<option>____________________________________________________</option>
			</select>
			</td>
		</tr>
		<tr>
			<td><a href="images/map>region">calendar</a>
			</td>
			<td colspan="5">
			<select name="region">
			<option selected><?php echo $_POST['region']; ?><option>
			<option value="ALU">Alcatel-Lucent (ALU)</option>
			<option value="AREA">Area /National</option>
			<option value="CATN">Carolinas / Tennessee</option>
			<option value="CTX">Central Texas</option>
			<option value="Device">Device</option>
			<option value="ERC">ERC</option>
			<option value="FL">Florida</option>
			<option value="GAAL">Georgia / Alabama</option>
			<option value="HGC">Houston Gulf Coast </option>
			<option value="SCTL">South Central</option>
			<option>____________________________________________________</option>
			</select>
			</td>
		</tr>
		<tr>
			<td colspan="2"><textarea class="description"><?php echo $_POST['description']; ?></textarea></td>
			</td>
		</tr>
	</table><br>
	<div style="text-align:center;"><input type="image" src="images/close_button.jpg" onclick="window.close()"></div>
	</div>

</body>

</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And that is why you get paid the large dollars!

Many thanks!
<|;-)