Wilest
Wilest

Reputation: 1860

php auto-save code

I have a page with 100 questions.

When the user submits the form the data gets saved to a table (on the next page).

What I want to do though is save the user's selected answers say every 30 seconds to a table before submitting the form.

Can someone please guide me in what direction to go?

How do I go about running the script say every 30 seconds?

Help will be greatly appreciated.

Upvotes: 0

Views: 2353

Answers (7)

CoreCoder
CoreCoder

Reputation: 419

<script type="text/javascript">
function func()
{
 // collect data like that
 //var your_data_array = {}
 // your_data_array.Ans1 = $('#someID').val();  
  $.ajax({  
            type: "POST",  
            url:"path/to/page/which/will/save_data.php",  
            data:your_data_array,
            //dataType:'json',

            beforeSend: function()
            {
            },
            success: function(resp)
            {  
                $("#activity").html(resp);
                /*$("#db").html(resp.db);
                $("#time").html(resp.time);*/
            }, 
            complete: function()
            {
            },
            error: function(e)
            {  
            alert('Error: ' + e);  
            }  
        }); 

  var t = setTimeout("func()",30000); 
}
var t = setTimeout("func()",30000);
</script>

add this to your head section after preapare according to your data func() function will be fire up after every 30 seconds.

Upvotes: 0

Cyclonecode
Cyclonecode

Reputation: 30071

The following code uses jQuery to send some data to a php file called store.php every second.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function callback() {
    // make ajax request
    $.ajax('store.php',{
       data:'<contains-the-data-to-store>',
       success:function() {

       }
    });
}
$(document).ready(function() {
    setInterval("callback()", 1000);
});
</script>

Upvotes: 1

Subodh
Subodh

Reputation: 2214

Using Javascript :

var refreshId = setInterval(function()
{
    //get your data here and make an ajax call to update your db table

}, 10000);

This code will run for every 10 seconds on your page.

Upvotes: 0

Narek
Narek

Reputation: 3823

function sendQuery(){
    AjaxArr = $('#formID *').serializeJSON();

    toAjax(AjaxArr,'yourPHPToSave.php');

    setTimeout('sendQuery',30000);
}

function toAjax(arr,ajaxAdr){
    JsHttpRequest.query(    siteAdr+ajaxAdr+'?x=c'+Math.random(), AjaxArr,
        function(result, errors) {
            if(errors)alert(errors);
            else{
                if(result['error'] && result['error']!='')alert(result['error']);
                if(result['callback'] && result['callback']!='')eval(result['callback']);
            }
        }
    );
}

jQuery.fn.serializeJSON=function() {
    var json = {};
    jQuery.map($(this).serializeArray(), function(n, i){
        json[n['name']] = n['value'];
    });
    return json;
}

Call sendQuery and retrive POST data in yourPHPToSave.php

UPDATE: You need JsHttpRequest library http://www.phpclasses.org/package/3637-PHP-Process-regular-and-file-upload-AJAX-requests.html and jQuery

Upvotes: 0

davidjwest
davidjwest

Reputation: 546

I don't think you can do this using PHP alone but there's an interesting jQuery function that sounds like what you need:

http://rikrikrik.com/jquery/autosave/

That uses cookies but perhaps you can modify it to use a database?

Upvotes: 1

Dominic
Dominic

Reputation: 31

I'd suggest using JS to run some code every 30 seconds ( setTimeout() ) which either stores your data in a cookie, or sends it to the server via ajax (though I would recommend cookies)

Upvotes: 0

Rich Bradshaw
Rich Bradshaw

Reputation: 72975

You'll need to use javascript.

You'll need to look up the following:

  1. How to post a form using AJAX.
  2. window.setInterval

I'd use jQuery, as it makes the JS easy for this.

Upvotes: 1

Related Questions