2

I am using ajaxForm to send data to php file which runs in background, and some js functions will be checking if php file is running asynchroniously. So in case of some failure(internet/electricity went off), I want my user be able to retry the last form submission. So I found some ajax retry functions as this, best way to retry ajax but it is on the same ajax request, i need to be able to retry my ajax from another function. Is it even possible? Given that I am storing passed values in form.

$(document).ready(function(){
$('#form').ajaxForm({
    target: '#ajax-response',
    success: function(){
 initAjaxCheck();
     }
});
Check();
});
So, basically lockcheck() checks if process is still runing after page refresh/reload, and if any failure was detected with prevous run, it should be able to retry the form submission.

Here's js scripts:

function Check(){
    $.ajax({
 type:"POST",
        url:"ajax.php",
        data:{'flag':'Check'},
        success:function(result){
         if (result=="1") {
      initAjaxCheck();
         } 
        }
    });
}



function initAjaxCheck(){
    $.ajax({
        type:"POST",
        url:"ajax.php",
        data:{'flag':'initProcCheck'},
        success:function(data){
         if (data=="1"){ 
  timerCheck=setInterval(function(){
      ajaxCheck(timerCheck);
  },1000);
            } else { //unsuccessful
            var toRepeat = confirm ("Last try was unsuccessful. Retry?");
            if (toRepeat){alert("retry");} else {alert("cancel");}
            }
        } 
    });
}

So if my user press RETRY, I should have to retry my ajaxForm submission.

amelie
  • 289
  • 3
  • 16
  • i think you can just call `initAjaxCheck()` everytime user press retry. Javascript function can call itself inside the function. this is called recursive function. – Jacob Goh Sep 26 '17 at 06:56
  • @JacobGoh will it pass my form values to async php? `initAjaxCheck` is just a function to check process, if state of `ajaxForm` is `success`. – amelie Sep 26 '17 at 06:59
  • 1
    sorry, i dun really understand your function. I dun know how your data flows either. but you can put the data inside a variable and check if the data exists before every ajax call. If the data is not there on resubmit, just use something like `$('#my_input_field').val()` to grab the form value 1 by 1 and pass the value into the ajax call. hope this helps. – Jacob Goh Sep 26 '17 at 07:03
  • @JacobGoh thanx! i'd help a lot. – amelie Sep 26 '17 at 07:46

1 Answers1

1

I'm a little confused with what exactly you are asking, however I believe I somewhat understand.

Trigger a form submission.

$('#form').trigger("submit");

or

var event = jQuery.Event("submit");
$('#form').trigger(event);

To implement this in your code, simply add it after your confirm function.

var toRepeat = confirm ("Last try was unsuccessful. Retry?");

if (toRepeat){
    alert("retry");
    $('#form').trigger("submit");
} else {
    alert("cancel");
}
Lars Peterson
  • 1,508
  • 1
  • 10
  • 27