简体   繁体   中英

submitting form with ajax jquery

i have a form where i am storing the time spent by users on site so for every user there is one id and my form is

<form id="mycash" name="form6" method="post" action="">
<input name="Time_Spent1"  value="'.$row['Time_Spent'].'"  id="Time_Spent1"        type="text" size="7" readonly="readonly" /><input type="button" name="task" id="task" value="submit">

i am using this javascript to submit the form using ajax .but it is not working.

<script language="javascript">$(document).ready(function(){
$("form#mycash").submit(function() {
var Time_Spent1     = $(\'#Time_Spent1\').attr(\'value\');
var task     = $(\'#task\').attr(\'value\');
    $.ajax({
        type: "POST",
        url: "page.tpl.php",
        data: "Time_Spent1="+ Time_Spent1 + "&\'{$client[\'id\']}\'="+ \'{$client[\'id\']}\'+ "&task="+ task,
        success: function(){
            $(\'form#mycash\').hide(function(){$(\'div.success\').fadeIn();});

        }
    });
return false;
});

});

i want to submit this form every 20 seconds automativcally . any suggestion where i am doing mistake

Why do you have '\\ in jQuery selector $(\\'#Time_Spent1\\') should be $('#Time_Spent1')., hope that will help. The data part you should leave as JSON format

$.ajax({
    type: "POST",
    url: "page.tpl.php",
    data: {"Time_Spent1":  Time_Spent1, "task" : task}

....

If you want to do automation submit, you should create a function and set interval to it

function submitform(){//Do something}

setInterval(submitform, 20000); //20k milisecond = 20secs

I don't see any automation. Try this:

(function submitForm(){
    setTimeout(function(){
        $("form#mycash").submit();
        submitForm();
    },20000);
}());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM