if else语句在AJAX内部成功无法正常工作

时间:2017-01-06 10:38:21

标签: php jquery ajax

在我的AJAX success函数中,有一个if / else语句。来自服务器端的响应是可以的,但是当我在这里检查时,if逻辑与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的逻辑不起作用(from不隐藏)而是我得到警报。< / p>

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

听起来你的回答是整数1,而不是字符串&#39; 1&#39;。您可以通过添加以下内容来仔细检查返回的内容:

console.log(response, typeof response)

你的考试:

if (response === '1') {

要求response是字符串&#39; 1&#39;,数字1将不匹配。因此,centre-upload-process2.php中的处理工作正常,记录添加到数据库中,并返回整数1.这不是Javascript所期望的字符串,因此它被解释为错误,并显示您的警报。

假设这是真的并且你得到一个整数,请更新你的JS以期望一个整数:

if (response === 1) {