javascript - Auto save form field inputs using jquery and ajax - Stack Overflow

I have a form with different input fields.So for very minute , the data entered by the user needs to be

I have a form with different input fields.So for very minute , the data entered by the user needs to be automatically stored in the database. Once the request is submitted , it will be directed to the struts file where the database interactions will be carried out .

What i have tried, I had set the timeout function to run every time the page is loaded

var timer;
$(document).ready(function() {
timer = setTimeout("autosave()", 60000); 
});

And in the autosave function , i am trying to post the input data to the designated URL

jQuery('form').each(function() {
        jQuery.ajax({
            url: "http://localhost:7002/submitStudent.do?requestType=auto&autosave=true",
            data: jQuery(this).serialize(),
            type: 'POST',
            success: function(data){
                if(data && data == 'success') {
                    alert("data saved");
                }else{
                }
            }
        }); 
    }); 
}
 }

And once the request is sent to the struts , it will be processed based on the requesttype and the data will be submitted.

But in my case , data doesn't get saved.

Kindly share your suggestions on what i am doing wrong and any other ways to do it ?

Thanks for your valuable suggestion and time..

FYI , i am a beginner in Jquery and ajax technologies

JSFIDDLE : jsfiddle

I have a form with different input fields.So for very minute , the data entered by the user needs to be automatically stored in the database. Once the request is submitted , it will be directed to the struts file where the database interactions will be carried out .

What i have tried, I had set the timeout function to run every time the page is loaded

var timer;
$(document).ready(function() {
timer = setTimeout("autosave()", 60000); 
});

And in the autosave function , i am trying to post the input data to the designated URL

jQuery('form').each(function() {
        jQuery.ajax({
            url: "http://localhost:7002/submitStudent.do?requestType=auto&autosave=true",
            data: jQuery(this).serialize(),
            type: 'POST',
            success: function(data){
                if(data && data == 'success') {
                    alert("data saved");
                }else{
                }
            }
        }); 
    }); 
}
 }

And once the request is sent to the struts , it will be processed based on the requesttype and the data will be submitted.

But in my case , data doesn't get saved.

Kindly share your suggestions on what i am doing wrong and any other ways to do it ?

Thanks for your valuable suggestion and time..

FYI , i am a beginner in Jquery and ajax technologies

JSFIDDLE : jsfiddle

Share Improve this question edited Apr 14, 2015 at 7:24 jaggs asked Apr 14, 2015 at 7:09 jaggsjaggs 3082 gold badges10 silver badges28 bronze badges 12
  • Is the data getting saved in you database? – Kushal Commented Apr 14, 2015 at 7:11
  • requestType="auto" write this without double quotes.. – Brijesh Bhatt Commented Apr 14, 2015 at 7:12
  • 2 setTimeout will not call every minute . – Nishit Maheta Commented Apr 14, 2015 at 7:12
  • 2 Something like this fiddle should do what is required. 6 seconds instead of 60 is for testing. – Regent Commented Apr 14, 2015 at 7:18
  • 1 @GuruprasadRao Modified it.Thanks – jaggs Commented Apr 14, 2015 at 7:46
 |  Show 7 more ments

2 Answers 2

Reset to default 3

I have made a fiddle according to your requirement.

var timer;

var fun = function autosave() {
    alert();
    jQuery('form').each(function () {
        jQuery.ajax({
            url: "http://localhost:7002/submitStudent.do?autosave=true",
            data: jQuery(this).serialize(),
            type: 'POST',
            success: function (data) {
                if (data && data == 'success') {
                    alert("data saved");
                } else {}
            }
        });
    });
}
$(document).ready(function () {
    setTimeout(fun, 1000);
    //setInterval(fun,1000);
});

You need to focus on two methods setTimeout and setInterval. setTimeout will call autosave() after 1 second of DOM loading but only once. setInterval will call autosave() after every 1 second repeatedly. You can read it here.

The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds. Tip: The function is only executed once. If you need to repeat execution, use the setInterval() method.

For more details on your ajax request you need to look at the console(F12) errors.

I remend that you use ajaxForm plugin

and in the autosave function just fire $('form').submit();

this is the fast and good way

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744375650a4571172.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信