TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-23-2008, 02:49 PM   #1 (permalink)
The Wanderer
 
Join Date: Oct 2008
Posts: 5
Thanks: 4
iory is on a distinguished road
Default saving to database after few seconds

dear all, im newbie here :p
I have this code, after user make a selection, he/she see the data that he/she selected, then, that data name will be automatically save into their record database. is it possible to make that, the record will be saved after user see for few seconds (ex: 5 seconds). so that it wont save immediately ( for consideration that, if user see the data for more than 5 seconds, that
means he/she is interested, then it should be saved automatically)

here is the code:
Code:
<?php
if ($registration>0){
 $qry="SELECT * FROM registration WHERE id_registration=".$registration" LIMIT 1";
 $exe=mysql_query($qry);
 $show=mysql_fetch_array($exe);
 if (mysql_num_rows($exe)==1){
 echo "<div class='divide'>&nbsp;&nbsp;++ ".$show['title_registration']." ++</div><br />";
  
$qry="INSERT INTO tb_history VALUES('',".$_SESSION['ID'].",'".$show['name_word']."',
'".$_SERVER[HTTP_REFERER]."','".date("Y-n-j H:i:s")."')";
?>
can someone please help me with this? :(
iory is offline  
Reply With Quote
Old 10-23-2008, 03:47 PM   #2 (permalink)
The Wanderer
 
Join Date: Oct 2008
Location: Florida
Posts: 5
Thanks: 1
chrisb is on a distinguished road
Default

The way I would do this, and it may not be the best... (no expert here) would be to use a javascript timer which grabs the data after X seconds and uses AJAX to send the data to a php script which records it to the database.

I apologize, but I cannot provide you with an example at this time.
chrisb is offline  
Reply With Quote
Old 10-23-2008, 03:51 PM   #3 (permalink)
The Wanderer
 
Join Date: Oct 2008
Posts: 5
Thanks: 4
iory is on a distinguished road
Default

i see, thanks for your suggestion , do you have any example for this (later is ok..im still trying to find other ways without using AJAX)? im kinda new in PHP, to move on to AJAX, i think it will be hard for me :(
iory is offline  
Reply With Quote
Old 10-23-2008, 09:36 PM   #4 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

The fact is that PHP is a Server-side script language which means that nothing is going to happen or execute without some Client-side intervention.

Unless you trigger the call yourself through GET or POST and such.

And in this case would JavaScript be great to handle that delayed saving.

Well you could use cronjobs to execute PHP on the server.

But that wouldent help the user.
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 10-24-2008, 02:48 AM   #5 (permalink)
The Wanderer
 
Join Date: Oct 2008
Posts: 5
Thanks: 4
iory is on a distinguished road
Default

i see..because i tried the script above, and it can save the "data name" automatically , thats why im thinking, it should be better if it can save after few seconds ( because if user really interested at looking the data name, he/she should spend some times looking at it )..so, javascript is another option to handle this case right? thanks for your suggestion :), im really appreciated it :)
iory is offline  
Reply With Quote
Old 10-24-2008, 08:27 AM   #6 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

Yes it may have been saved "automaticly" but not without the user loading the page.

Cause thats what the user would have to do if your not going with JavaScript.

Cause PHP in itself canīt magicly induce a page load and run the code for saving the data.

You would need some form of user intervention.

Thats why you would need to use JavaScript to make the "Call" to the server using AJAX and calling your script.

In other words the user must do something to make the page load.
Like click a link or send a form.
At least if your not going to use JavaScript.

Thats the bottom line. Because as said before. PHP is Server-Side.

and JavaScript is Client-Side.

good luck :)
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
The Following User Says Thank You to EyeDentify For This Useful Post:
iory (10-24-2008)
Old 10-24-2008, 10:18 AM   #7 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 24
Thanks: 13
sidisinsane is on a distinguished road
Default

The PHP-function "sleep()" should do it. Here's just a (somewhat nonsensical) sample script that delays a defined action while displaying a countdown, without the use of javascript. You should get the idea ...

Code:
function delayAction($action, $delay=5, $frequency=1, $confirm=FALSE)
{
	ob_implicit_flush(TRUE);

	
	for($i = 0; $i < $delay; $i++)
	{
		// Important! Style boxes and make sure the z-index rises with each call. Also set a background-color so that underlying layer is covered.
		$countdown_pat = "<div style=\"position:absolute;z-index:".$i.";background:#FFF;\">%s</div>";
		$confirm_pat = "<div style=\"position:absolute;z-index:".$i.";background:#FFF;\"><h2>Really? <a href=\"%s\">Confirm</a> / <a href=\"%s\">Undo</a></h2></div>";
		$redirect_pat = "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=%s\">";
		
		// "Pulsate" until delay is reached. Then perform action.
		if($i < $delay-1)
		{
			// Display countdown
			printf($countdown_pat, date('h:i:s'));
			
			// "Pulsate"
			sleep($frequency);
		}else{
			if($confirm == FALSE)
			{
				// Redirect
				printf($redirect_pat, $action);
			}else{
				// Display confirmation-link
				printf($confirm_pat, $action, $_SERVER['PHP_SELF']);
			}
		}
		
	}
}

// Test
$frequency = 1; // Set "pulse" in seconds
$delay = 5; // Set delay + 1 in seconds
$action = "http://boldeagle.org"; // Set action. (Here I'm setting the location for a meta-refresh/link)

// Execute with custom settings (with confirmation-link)
delayAction($action, $delay, $frequency, 1);

// Execute with default settings (without confirmation-link)
//delayAction($action);
sidisinsane is offline  
Reply With Quote
The Following User Says Thank You to sidisinsane For This Useful Post:
iory (10-24-2008)
Old 10-24-2008, 03:35 PM   #8 (permalink)
The Wanderer
 
Join Date: Oct 2008
Location: Florida
Posts: 5
Thanks: 1
chrisb is on a distinguished road
Default

I have put together a very simple example using the method I described. Please see Timer Test Page .. After 5 seconds it updates and says it was updated. If you view source you will see all of the javascript (AJAX) require to do it. You should be able to pick it apart to make it do what you want.

Please note that I am just using basic knowledge gathered from resources. There is proably a better way to handle it, but eh I said I could do it and here is how :P

timer.php Page Code:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function startTimer()
{
var t=setTimeout(updateInfo,5000);
}

function updateInfo() {
		var data = document.getElementById('timer_registration').value;
		sndReq( data );		
	}//~

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}//~

var http = createRequestObject();

function sndReq(action) {
	//note( 'sndReq: ' + action );
    http.open('get', 'updater.php?action='+action);
    http.onreadystatechange = handleResponse;
    http.send(null);
}//~

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
	var obj = document.getElementById('updatediv');
	obj.innerHTML = response;

    }
   
}//~



</script>
</head>

<body onLoad="startTimer();">
<div id="updatediv"></div>
<form action="#" method="get">
<input name="timer_registration" type="text" id="timer_registration" value="Example Title Registration" size="45"  />
</form>
</body>
</html>
updater.php Code:
PHP Code:
if ($_GET['action']) {
$data $_GET['action'];

// Make database update query....

// If update went ok
echo "Updated Data with $data";

chrisb is offline  
Reply With Quote
The Following User Says Thank You to chrisb For This Useful Post:
iory (10-24-2008)
Old 10-24-2008, 03:50 PM   #9 (permalink)
The Wanderer
 
Join Date: Oct 2008
Posts: 5
Thanks: 4
iory is on a distinguished road
Default

thx @chrisb and @sidisinsane :D , you guys really helps me alot!! i will try to use and report the result :D , goshh..im newbie in this field T_T
iory is offline  
Reply With Quote
Old 10-24-2008, 05:07 PM   #10 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Dont use browser detection, instead use object detection:
javascript Code:
function createRequestObject() {
    try{
        return new XMLHttpRequest();
    } catch (e) {
        try{
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                return false;
            }
        }
    }
}

For more information: Javascript - Object detection
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following 2 Users Say Thank You to sketchMedia For This Useful Post:
chrisb (10-24-2008), iory (10-24-2008)
Old 10-24-2008, 06:52 PM   #11 (permalink)
The Wanderer
 
Join Date: Oct 2008
Location: Florida
Posts: 5
Thanks: 1
chrisb is on a distinguished road
Default

Thank you, I will add that to my library. :)
chrisb is offline  
Reply With Quote
Old 10-27-2008, 01:26 PM   #12 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

Why not use Prototype JS for example.

They have a nifty function in there that would be great in this case.

Take a look.
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 10-28-2008, 01:38 PM   #13 (permalink)
The Wanderer
 
Join Date: Oct 2008
Posts: 5
Thanks: 4
iory is on a distinguished road
Default

thanks for all of you who shared your idea here, im still trying to combine above function, but still couldnt get result as i expected before,i think that i got some mistakes in my code..T_T i think java script is one of good option my case :) i will put updates in my thread, and if succeed, i will put the results for others who might have same problems :p
iory is offline  
Reply With Quote
Old 10-28-2008, 01:59 PM   #14 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

and don't forget to secure your scripts ;)
codefreek is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:46 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design