Solved

Clearing table values once user navigates away from page

Posted on 2011-03-04
10
465 Views
Last Modified: 2012-05-11
Hi Experts,

I have created a webpage that allows a user to enter table data and the table automatically refreshes using ajax.

However its only going to be used for proof of concept. So how can i clear any table entries a user has made once that user either leaves the website or refreshes the browser?

Many Thanks,
Dean

URL: http://www.the-lees.co.uk/site1/ajax_insert.php
0
Comment
Question by:deanlee17
10 Comments
 
LVL 4

Expert Comment

by:Tomun
ID: 35037476
Just don't save it in your database.
0
 
LVL 4

Expert Comment

by:LAMASE
ID: 35037531
Since HTTP is stateless, you cannot "know" is a person is still on your page, or has his pc crashed or shutted duwn.
Normally sessions way of thinking is: "if the user comes back within x minutes, I keep his session active".
So what can you do is delete every entries not updated within the lapse you use for ajax updating. Keep in mind that the user can have temporary internet connection problems, and in this case you will think that the user gone away and delete his data...

An html trick can be using
<body onBeforeUnload="function()">

that is triggered when the user close the page. You can perform an ajax call (syncronous is better) that erases all the data, plus a timed script that deletes all data older than x minutes.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35037694
Store the data in serailized format in the session rather than the database and then when the user closes the session the data is wiped automatically.

http://www.php.net/serialize
http://www.php.net/unserialize
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:deanlee17
ID: 35038192
bportlock can you still print the data from a session so as its displayed as if its coming from a database? Click link in my original post to see what im talking about.

Thanks,
Dean
0
 

Author Comment

by:deanlee17
ID: 35038278
Actually, thinking about it, is it possible to have a button that uses AJAX to empty the table without a page refresh?
0
 
LVL 14

Expert Comment

by:Scott Madeira
ID: 35039189
If you want the ability to have a reset button on your form to clear the data then why don't you just use an HTML reset button:

<INPUT TYPE=RESET Value="Clear Form">

If that is all you want then you don't need AJAX to do it.
0
 
LVL 4

Expert Comment

by:Tomun
ID: 35039367
smadeira: That would clear the form but not remove the things from the database that he's saved. Refreshing would bring it all back.

deanlee17:
Without seeing you php code I'm having to guess what your doing. I guess your ajax_data.php file saves the input to the database, then pulls it out again and echoes a <tr>...</tr> line with the data in it.

You could construct the table row directly from the form input without having to save it in the database at all. ajax_data.php seems to be broken at the moment. Show us the code if you want some help with it.
0
 

Author Comment

by:deanlee17
ID: 35039444
Hi Tonun,

I have attached the code that constructs the page, ive now set the page back to the way it was....

http://www.the-lees.co.uk/site1/ajax_insert.php

Thanks,
Dean
<?
//connect to database
$dbhost = 'mysql01.myhostcp.com';
$dbuser = 'deans_user1';
$dbpass = 'qwerty123';
$dbname = 'deansghot5722com5233_deansdatabase';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);

//NOTE: MAKE SURE YOU DO YOUR OWN APPROPRIATE SERVERSIDE ERROR CHECKING HERE!!!
if(!empty($_POST) && isset($_POST))
{
    $emptyvariables = false;

    if(!empty($_POST)){
        foreach($_POST as $key => $val){
            if(empty($_POST[$key])){
                echo"Please insert $key<br />";
                $emptyvariables = true;
                }
        }
    }
    
    if($emptyvariables == false){
    


        //make variables safe to insert
        $id = mysql_real_escape_string($_POST['id']);
        $Forename = mysql_real_escape_string($_POST['Forename']);
        $Surname = mysql_real_escape_string($_POST['Surname']);
        $Arrival_Date = mysql_real_escape_string($_POST['Arrival_Date']);
        $Departure_Date = mysql_real_escape_string($_POST['Departure_Date']);
        $RoomNo = mysql_real_escape_string($_POST['RoomNo']);
        $Tel = mysql_real_escape_string($_POST['Tel']);

        //query to insert data into table
        $sql = "
            INSERT INTO bookings
            SET
            id = '$id',
            Forename = '$Forename',
            Surname = '$Surname',
            Arrival_Date = '$Arrival_Date',
            Departure_Date = '$Departure_Date',
            RoomNo = '$RoomNo',
            Tel = '$Tel'";
            
            echo"$query";
        $result = mysql_query($sql);
        if(!$result)
        {
            echo "Failed to insert record<br>Please Ensure booking does not currently exist";
        }
        else
        {
            echo "Record inserted successfully";
        }
    }
}



?>


<table width="300" border="1">
	<tr>
		<td><b>ID</b></td>
		<td><b>Forename</b></td>
		<td><b>Surname</b></td>
		<td><b>Arrival_Date</b></td>
		<td><b>Departure_Date</b></td>
		<td><b>RoomNo</b></td>
		<td><b>Tel</b></td>

	</tr>
	<?
	//show data from tables
	$sql = "
		SELECT *
		FROM bookings
		ORDER BY ID ASC
		LIMIT 20";
	$result = mysql_query($sql);
	while($row = mysql_fetch_array($result))
	{
	//print data
	?>
	<tr>
		<td><? echo $row['id']; ?></td>
		<td><? echo $row['Forename']; ?></td>
		<td><? echo $row['Surname']; ?></td>
		<td><? echo $row['Arrival_Date']; ?></td>
		<td><? echo $row['Departure_Date']; ?></td>
		<td><? echo $row['RoomNo']; ?></td>
		<td><? echo $row['Tel']; ?></td>
	</tr>
	<?
	}
	?>
</table>

Open in new window

0
 
LVL 4

Accepted Solution

by:
Tomun earned 500 total points
ID: 35039853
Try it like this:

<?
//connect to database
$dbhost = 'mysql01.myhostcp.com';
$dbuser = 'deans_user1';
$dbpass = 'qwerty123';
$dbname = 'deansghot5722com5233_deansdatabase';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);

//NOTE: MAKE SURE YOU DO YOUR OWN APPROPRIATE SERVERSIDE ERROR CHECKING HERE!!!
if(!empty($_POST) && isset($_POST))
{
    $emptyvariables = false;

    if(!empty($_POST)){
        foreach($_POST as $key => $val){
            if(empty($_POST[$key])){
                echo"Please insert $key<br />";
                $emptyvariables = true;
                }
        }
    }
    
    if($emptyvariables == false){
    


        //make variables safe to insert
        $id = mysql_real_escape_string($_POST['id']);
        $Forename = mysql_real_escape_string($_POST['Forename']);
        $Surname = mysql_real_escape_string($_POST['Surname']);
        $Arrival_Date = mysql_real_escape_string($_POST['Arrival_Date']);
        $Departure_Date = mysql_real_escape_string($_POST['Departure_Date']);
        $RoomNo = mysql_real_escape_string($_POST['RoomNo']);
        $Tel = mysql_real_escape_string($_POST['Tel']);

        //query to insert data into table
        $sql = "
            INSERT INTO bookings
            SET
            id = '$id',
            Forename = '$Forename',
            Surname = '$Surname',
            Arrival_Date = '$Arrival_Date',
            Departure_Date = '$Departure_Date',
            RoomNo = '$RoomNo',
            Tel = '$Tel'";
            
            echo"$query";
        /*$result = mysql_query($sql);
        if(!$result)
        {
            echo "Failed to insert record<br>Please Ensure booking does not currently exist";
        }
        else
        {*/
			echo "<td>$id</td><td>$Forename</td>$Surname<td>$Arrival_Date</td><td>$Departure_Date</td><td>$RoomNo</td><td>$Tel</td>";
        //}
    }
}

?>

Open in new window


For this to work you'll need to make two changes. First your ajax_insert.php should load the initial rows from the database and put them in the table as ajax_data.php will now only output the new row. Secondly instead of replacing the whole table just add the new row by calling jquery.form like this:

$(document).ready(function() {
    $('#myForm').ajaxForm({
      success: function(responseText) {
           $('#showdata tr:last').after('<tr>' + responseText + '</tr>');
      }
    });
  });

Open in new window

or something like that.
0
 

Author Comment

by:deanlee17
ID: 35040129
Thanks very much
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question