2017-07-01 9 views
0

悪い英語の書き込みのために申し訳ありません。
私は連鎖するいくつかのajaxコードを記述します。
最初のモーダル、最初に成功した場合は、モーダルとセカンドを非表示にして、別のajaxを送信してください。
JS
JSON入力の予期しない終了は、PHPで

private function phoneVerify($mobile) 
{ 
    header('Content-Type: application/json'); 
    global $mysqli; 
    $update = 0;// 0 insert/1 update 
    $error = 0;//0 no error/ 1 mobile exist but time less than 1/2 mobile verify 
    $pattern = preg_match("/^9[0-9]{9}$/", $mobile); 
    $row = $mysqli->query("SELECT * FROM `bv_sms` WHERE `mobile`='" . $mobile . "'"); 
    if ($mobileEx = $row->fetch_assoc()) { 

     if ($mobileEx['status'] == 0) { 

      $now = new DateTime('now'); 
      $create_at = new DateTime($mobileEx['create_at']); 
      $interval = $now->diff($create_at); 
      if ($interval->i < 1) { 
       $error = 1; 
      }else { 
       $update = 1; 
      } 
     } else { 
      $error = 2; 
     } 
    } 

    return array('error' => $error, 'pattern' => $pattern,'update' => $update); 
} 

public function sendVerifyCode() 
{ 
    header('Content-Type: application/json'); 
    global $bsiCore; 
    global $mysqli; 
    $errorcode = 0;//0 no error/ 1 mobile exist but time less than 1/ 2 mobile vrify/3 mobile number wrong 
    $msg = ""; 
    $mobile = $bsiCore->ClearInput($_POST['mobile']); 
    $mobileVer = $this->phoneVerify($mobile); 
    if ($mobileVer['pattern'] == 1) { 

     if ($mobileVer['error'] == 0) { 
      $token = rand(10000, 99999); 
      try { 

       //@TODO set if result ok then send query to database 
       if($mobileVer['update'] == 0){ 
        $mysqli->query("INSERT INTO `bv_sms` (`mobile`,`token`) VALUES ('" . $mobile . "','" . $token . "')"); 
       }else { 
        $mysqli->query("UPDATE `bv_sms` SET `token`='" .$token. "' WHERE `mobile`='".$mobile."'"); 
       } 

      } catch (ApiException $e) { 
       //$msg = "به دلیل بروز خطا در ارسال لطفا یک دقیقه بعد تلاش کنید."; 
      } catch (HttpException $e) { 
       //$msg = "به دلیل بروز خطا در ارسال لطفا یک دقیقه بعد تلاش کنید."; 
      } 

     } else { 
      if ($mobileVer['error'] == 1) { 
       $msg = "باید از ارسال کد اول یک دقیقه گذشته باشد."; 
       $errorcode = 1; 
      } else { 
       $msg = "این شماره قبلا تایید شده است."; 
       $errorcode = 2; 
      } 
     } 
    } else { 
     $msg = "شماره ی وارد شده معتبر نمیباشد."; 
     $errorcode = 3; 

    } 

    $strmsg = '<i style="color:#F00;">' . $msg . '</i>'; 
    echo json_encode(array("mobile" => $mobile, "errorcode" => $errorcode, "strmsg" => $strmsg)); 
} 

private function abc($a,$b){//$a = phone number get whit $_post['mobile'] $b= token get same 
    header('Content-Type: application/json'); 
    global $bsiCore; 
    global $mysqli; 
    $mobile = $bsiCore->ClearInput(intval($a)); 
    $token = $bsiCore->ClearInput(intval($b)); 
    $row = $mysqli->query("SELECT FROM `bv_sms` AS `bs` WHERE `bs`.`mobile`='" .$mobile. "' AND `bs`.`token`='" .$token. "' AND `bs`.`create_at` < (NOW() - INTERVAL 1 MINUTE)"); 
    if($row->fetch_assoc()){ 
     $mysqli->query("UPDATE `bv_sms` AS `bs` SET `bs`.`status`=1 WHERE `bs`.`mobile`='" .$mobile. "' AND `bs`.`token`='" .$token. "'"); 
     return true; 
    } 
    return false; 
} 
public function checkVerifyCode($a,$b) 
{ 
    header('Content-Type: application/json'); 

    $errorcode = 1;//0 no error/ 1 
    $msg = "کد تایید نامعتبر است."; 

    if($this->abc($a,$b)){ 
     $msg = "تایید شد."; 
     $errorcode = 0; 
    } 


    $strmsg = '<i style="color:#F00;">' . $msg . '</i>'; 
    return json_encode(array("errorcode" => $errorcode, "strmsg" => $strmsg)); 

} 

最初のAjaxがお送り

var timer = new Timer({ 
tick: 1, 
ontick: function (s) { 
    $('.timer').html(Math.round(s/1000) - 1); 
}, 
onstart: function() { 
    $('.text-white').prop("disabled", true); 
}, 
onstop: function() { 
}, 
onpause: function() { 
    console.log('timer set on pause') 
}, 
onend: function() { 
    $('.text-white').prop("disabled", false); 
} 
}); 

(function ($, timer) { 

$('#sendCode').on("click", function() { 

    var a = $.ajax({ 
     type: 'post', 
     url: 'ajax-processor.php', 
     data: { 
      mobile: $('#agent_mobile').val(), 
      actioncode: 23 
     }, 
     dataType: "json", 
     success: function (response) { 

      switch (response.errorcode) { 
       case 0://every things ok 
        timer.start(50); 
        $('.step1').hide(); 
        $('.step2').show(); 
        $('.verifySend').on('click', function() { 
         b = a.then(function (daat) { 
          return $.ajax({ 
           type: 'post', 
           url: 'ajax-processor.php', 
           dataType: "json", 
           data: { mobile: daat.mobile , token: $('#verifycode-form-field').val() , actioncode: 24}, 
           success: function (response2) { 
            $('.msg').html(response2.strmsg).show(); 
            console.log(response2); 
           }, 
           error: function (xhr, status, error) { 
            console.log(status); 
            console.log(error); //verify.js:51 
           } 
          }); 
         }); 
        }); 
        $('.changeNumber').on('click', function() { 
         $('.step1').show(); 
         $('.msg').hide(); 
         $('.step2').hide(); 
        }); 
        $('.text-white').on('click', function() { 
         timer.start(50); 
         $('.step1').show(); 
         $('.msg').hide(); 
         $('.step2').hide(); 
        }); 
        break; 
       case 1://time less than 1min 
        $('.msg').html(response.strmsg).show(); 

        break; 
       case 2://mobile verify 
        $('.msg').html(response.strmsg).show(); 
        break; 
       case 3://wrong number 
        $('.msg').html(response.strmsg).show(); 
        break; 
       default: 
        console.log("contact us"); 
      } 

     } 
    }); 
}); 

})(jQuery, timer); 

PHPファイルをファイルに成功
しかしsecound AJAXは、JSONが、応答は空とコンソールでこのエラーログとして送ります

parsererror 
SyntaxError: Unexpected end of JSON input verify.js:51 
at JSON.parse (<anonymous>) 
at n.parseJSON (jquery-1.12.4.min.js:4) 
at Xb (jquery-1.12.4.min.js:4) 
at y (jquery-1.12.4.min.js:4) 
at XMLHttpRequest.c (jquery-1.12.4.min.js:4) 
+1

幸せになるあなたはJSONとしてレスポンスを返すが、使用していませんアレイ。 'json_encode()'を使って応答をjson –

+0

@PankajMakwanaとして送信しますか? – omidmehr

+0

応答にはどの機能を使用しますか? –

答えて

関連する問題