2017-01-06 10 views
0

私のAJAX success関数の中には、if/elseという文があります。サーバー側からの応答はOKですが、ここで確認するとifロジックがelseロジックと一緒に実行されます。どちらも同じ時間に実行されますが、これは私にとってはちょっと混乱しています。AJAXの正常な動作でelse文が正常に動作しない場合

$.ajax({ 
     url: 'centre-upload-process2.php', 
     type: 'POST', 
     data: formData, 
     async: false, 
     success: function (response) { 

     if(response == 'success') { 
      $('#centreformupload').hide(); 
      $('#centreformpayment').show(); 
      console.log(typeof response); 
     } else { 
      alert("Already present"); 
      console.log(typeof response); 
     } 


     }, 

     contentType: false, 
     processData: false 
}); 

PHP

<?php require_once '../core/class_init.php'; ?> 

<?php 

$centre_uploads = new dbhandler(); 


$flag = false; 
$client_id = $_POST['client_id']; 
$r_id = $_POST['r_id']; 

$path = new uploads(); 

$payment_data = $centre_uploads->all_data('payment_dtls'); 
$i = 0; 
foreach($payment_data as $pd) { 



    if($payment_data[$i]->fy == input::get('fy')) { 

     $flag = true; 

    } 
} 







if(isset($_POST) && isset($_FILES)) { 


//$r_id = $_POST['r_id']; 


$inputtext2 = $_POST['inputtext2']; 
$inputfile2 = $path->upload_image('inputfile2'); 

$inputtext3 = $_POST['inputtext3']; 
$inputfile3 = $path->upload_image('inputfile3'); 

$inputtext4 = $_POST['inputtext4']; 
$inputfile4 = $path->upload_image('inputfile4'); 


if(empty($inputtext2) && empty($inputfile2)) { 

    $inputtext2 = 'NA'; 
    $inputfile2 = 'NA'; 


} 

if(empty($inputtext3) && empty($inputfile3)) { 

    $inputtext3 = 'NA'; 
    $inputfile3 = 'NA'; 


} 


if(empty($inputtext4) && empty($inputfile4)) { 

    $inputtext4 = 'NA'; 
    $inputfile4 = 'NA'; 


} 




if(!$flag) { 
    try{ 
     $centre_uploads->create('payment_dtls' , array(

      'r_id' => $r_id, 
      'client_id' => $client_id, 
      'fy' => input::get('fy'), 
      'ay' => input::get('ay') 
     )); 

     $centre_uploads->create('upload_dtls' , array(
      'r_id' => $r_id, 
      'client_id' => $client_id, 
      'pan_file' => $path->upload_image('pan_file'), 
      'bank_file' => $path->upload_image('bank_file'), 
      'insurance_file' => $path->upload_image('insurance_file'), 
      'title1' => $inputtext2, 
      'document1_file' => $inputfile2, 
      'title2' => $inputtext3, 
      'document2_file' => $inputfile3, 
      'title3' => $inputtext4, 
      'document3_file' => $inputfile4, 
      'purpose' => input::get('purpose') 
     )); 


     echo 'success'; 



    }catch(Exception $e){ 
     die($e->getMessage()); 
    } 

} else { 
    echo 'denied'; 
} 


} 

応答は1、データがデータベースに挿入されますが、私はまたalert、ここ応答1に書かれたロジックが動作しない(からは隠さない取得された場合)代わりに私は警戒を得る。

enter image description here

enter image description here

+4

は値 ' '1''または' 1 'のですか?つまり文字列または整数 –

+0

'typeof'を使用して、応答の種類を判断します(Rory McCrossan氏)。 – Ionut

+0

「成功」や「拒否」などのプレーンな文字列を使用しようとしました....まだ使用していません –

答えて

0

あなたの応答が整数1、ない文字列であるようなサウンドは '1'。あなたは追加することによって返されるまさに二重チェックできます:

console.log(response, typeof response) 

あなたのテスト:

if (response === '1') { 

responseが文字列である必要があり、「1」、数字の1が一致しません。したがって、centre-upload-process2.phpでの処理はうまくいきます。レコードはDBに追加され、整数1が返されます。これはJavascriptが期待する文字列ではないため、エラーと解釈され、警告が表示されます。整数を期待するあなたのJSを更新し、これが本当であり、あなたが戻って整数を取得していると仮定すると、

if (response === 1) { 
+0

なぜdownvoteですか? –

関連する問題