我的 php jquery ajax 表单请求有时会触发两次,可能是 10 个请求中的 2 个

问题描述 投票:0回答:0

以下是我提交表格的代码。 发送的 10 个请求中有 8 个工作正常,而其他请求会触发两次或三次,但会返回一个响应以显示甜蜜的警报。 我在发送请求之前使用甜蜜的警报确认,以确保在处理之前不会在按钮中单击两次。但问题依然存在,请进一步协助

$(document).on('click','#userForm',function(e) {
        e.preventDefault();
    
     var subscriptio = $('#subscription option:selected').text();
  var subscripti = $('#product option:selected').text();
    swal.fire({
                          title: 'Dear  $username',
                          text: 'Should we process your ' +subscriptio+ ' request?',
                          icon: 'question',
                          showCancelButton: true,
                          confirmButtonColor: '#3085d6',
                          cancelButtonColor: '#d33',
                          confirmButtonText: 'Yes, Continue',
             })
        .then((result) => {   if (result.value){

                            var data = $('#user_form').serialize();
console.log(data);
$.ajax({
    data: data,
    type: 'POST',
    url: 'action.php',
    cache: false, })
    .done(function(data){  
 psrt();
 $('#security').val('');
    var dataResult = JSON.parse(data);

    if (dataResult.status=='successful'){  swal.fire(dataResult.status, dataResult.message, dataResult.icon); }
    else { swal.fire(dataResult.status, dataResult.message, dataResult.icon); 
   }     

    })
    .fail(function(){
    swal.fire('Oops!', 'Something went wrong, kindly contact Admin', 'error'); 
    });
    } 
        }) 
}); 



</script>







// **My actions.php code** all variables used inside the function postrayer are correctly declared without any error
*//$wizz is an array of numbers exploded for verification*

      $mobiler = isset($_POST['mobile']) ? isints($_POST['mobile']) : '';

      $mobile = in_array($mobiler, $wizz)? false :$mobiler;

      

  _//Code to declined and **exit** with response if $mobile.is false is declared here_
 

    $postrayer = begins($userid, $wallet, $chan, $mypin,  $mobile, $amountsplit);  

        If ($postrayer){           
*//apiFunction is a function to send http request and it is being declared below*

    function apiFunction ($url,$payload,$token){

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);  

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = [

     "Authorization:$token",

    'Content-Type: application/json'

];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$dataapi = curl_exec($ch);

curl_close($ch);

    

    return $dataapi;
    }

$fg = apiFunction ($url,$payload,$token);


           $api_result = json_decode($fg,true);

    if(!empty($api_result)){
    If($api_result['Status'] == 'successful' ){

  // I echo my result to sweet alert
    }
    }  
        }```

8 out of 10 form request will work perfectly, but the remaining will send request twice or thrice
 I can't monitor this from the network tab because it seldomly happens and I don't know when precisely it will. 
And this is for transactional purpose
Please help 
php jquery ajax sweetalert
© www.soinside.com 2019 - 2024. All rights reserved.