TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   saving to database after few seconds (http://www.talkphp.com/absolute-beginners/3514-saving-database-after-few-seconds.html)

iory 10-23-2008 02:49 PM

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? :(

chrisb 10-23-2008 03:47 PM

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.

iory 10-23-2008 03:51 PM

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 :(

EyeDentify 10-23-2008 09:36 PM

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.

iory 10-24-2008 02:48 AM

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 :)

EyeDentify 10-24-2008 08:27 AM

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 :)

sidisinsane 10-24-2008 10:18 AM

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);


chrisb 10-24-2008 03:35 PM

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";



iory 10-24-2008 03:50 PM

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

sketchMedia 10-24-2008 05:07 PM

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

chrisb 10-24-2008 06:52 PM

Thank you, I will add that to my library. :)

EyeDentify 10-27-2008 01:26 PM

Why not use Prototype JS for example.

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

Take a look.

iory 10-28-2008 01:38 PM

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

codefreek 10-28-2008 01:59 PM

and don't forget to secure your scripts ;)


All times are GMT. The time now is 03:36 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0