Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onunload event problem

Status
Not open for further replies.

akaballa123

Technical User
Apr 29, 2008
46
US
Hi There,

Im working on creating an ajax request that will be called through the onunload event in the body tag.

I have set a unique id for my form so that each time the form opens a new id is created. This automatically gets submitted to the database. However, if the user exits the page instaed of submitting it, then I will have to delete the unique id row from the database table.

I have created the code for this, but upon debugging, the unique id does not get delete once i exit the form. I also placed a confirm function in the onunload to see if that works, but the confirm popup does not appear either.

This is the ajax/javascript code that i used to delete the id.

Code:
Code:
function secIns()
{
	<!-- 
//Browser Support Code

	var ajaxRequest;  // The variable that makes Ajax possible!
	var err = 0;
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser is not compatible!");
				return false;
			
			}
		}
	}
	// Create a function that will receive data sent from the server
	    ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			document.sessionForm.submitted.value = ajaxRequest.responseText;
		}
	    }
     //	ajaxRequest.open("GET", "serverTime.php", true);
     //	ajaxRequest.send(null); 


        //-->
  

	    var secID = document.getElementById('secID').value;
     	var queryString = "?secID=" + secID;
	    ajaxRequest.open("GET", "securityIDupdate.php" + queryString, true);
	    ajaxRequest.send(null);
}

this is where I call the onunload function:

Code:

Code:
<body onunload="if(confirm('Are you sure you want to exit?'){secIns();})">

lastly this is the php code/page that is called to delete from the table ( i am not sure if i should put this, but i am taking a chance )

Code:
Code:
<html>
<body>
<form name="secForm"  id="secForm">
<?php
include("mysqlConnection.php");

$secid = $_GET['secID'];

@mysql_query("DELETE FROM trainings WHERE securityId = $secid")or die('Did not delete');

include("mysqlConnClose.php")
?>
</form>
</body>
</html>
 
You have a mis-matched bracket. This:

Code:
if(confirm('Are you sure you want to exit?') {secIns();}[!])[/!]

should be:

Code:
if(confirm('Are you sure you want to exit?')[!])[/!] {secIns();}

or even just:

Code:
if(confirm('Are you sure you want to exit?')) secIns();

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top