0

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.

lll
12.9k3 gold badges42 silver badges60 bronze badges
asked Feb 9, 2012 at 12:48
1
  • 1
    You'll need JavaScritp for this (AJAX) to make a call to the webserver every x seconds. Commented Feb 9, 2012 at 12:51

7 Answers 7

1

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.

answered Feb 9, 2012 at 12:50
Sign up to request clarification or add additional context in comments.

Comments

1

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?

answered Feb 9, 2012 at 12:54

Comments

1

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>
answered Feb 9, 2012 at 13:01

Comments

0

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)

answered Feb 9, 2012 at 12:53

Comments

0
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

answered Feb 9, 2012 at 12:56

Comments

0

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.

answered Feb 9, 2012 at 12:57

Comments

0
<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.

answered Feb 9, 2012 at 13:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.