Solved

Clearing table values once user navigates away from page

Posted on 2011-03-04
10
462 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now