JavaScript收到错误返回(我猜)

时间:2017-05-30 05:33:32

标签: javascript php ajax


我的.php返回数字,然后我知道我得到了什么错误或成功,但是js没有得到正确的回报而且我不知道为什么。

.php

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/resources/config.php';
if(!empty($_POST)){
    $query = "SELECT 1 FROM suppliers WHERE name = :name"; 
    $query_params = array(':name' => $_POST['suppliername']); 
    try{ 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex){
        die("Failed to run query: " . $ex->getMessage()); 
    } 
    $row = $stmt->fetch(); 
    if($row){
        //Nome já cadastrado
        echo 01;
        return true;
    }
    if(!empty($_POST['suppliersocialname'])){
        $query = "SELECT 1 FROM suppliers WHERE social_name = :social_name"; 
        $query_params = array(':social_name' => $_POST['suppliersocialname']); 
        try{
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex){ 
            die("Failed to run query: " . $ex->getMessage()); 
        } 
        $row = $stmt->fetch(); 
        if($row){
            //Razão social já cadastrada
            echo 02; 
            return true;
        }
    }
    if(!empty($_POST['supplierfantasyname'])){
        $query = "SELECT 1 FROM suppliers WHERE fantasy_name = :fantasy_name"; 
        $query_params = array(':fantasy_name' => $_POST['supplierfantasyname']); 
        try{
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex){ 
            die("Failed to run query: " . $ex->getMessage()); 
        } 
        $row = $stmt->fetch(); 
        if($row){
            //Nome fantasia já cadastrado
            echo 03; 
            return true;
        }
    }
    if(!empty($_POST['suppliercnpj'])){
        $query = "SELECT 1 FROM suppliers WHERE cnpj = :cnpj"; 
        $query_params = array(':cnpj' => $_POST['suppliercnpj']); 
        try{
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex){ 
            die("Failed to run query: " . $ex->getMessage()); 
        } 
        $row = $stmt->fetch(); 
        if($row){
            //CNPJ já cadastrado
            echo 04; 
            return true;
        }
    }
    $query = " 
        INSERT INTO suppliers ( 
            name,
            social_name, 
            fantasy_name,
            cnpj,
            address,
            phone1,
            phone2,
            phone3,
            email1,
            email2,
            contact,
            paymenttype,
            bank1,
            bank_agency1,
            bank_account1,
            bank_accounttype
        ) VALUES ( 
            :name,
            :social_name, 
            :fantasy_name,
            :cnpj,
            :address,
            :phone1,
            :phone2,
            :phone3,
            :email1,
            :email2,
            :contact,
            :paymenttype,
            :bank1,
            :bank_agency1,
            :bank_account1,
            :bank_accounttype
        )
    "; 
    $query_params = array( 
        ':name' => $_POST['suppliername'], 
        ':social_name' => $_POST['suppliersocialname'], 
        ':fantasy_name' => $_POST['supplierfantasyname'], 
        ':cnpj' => $_POST['suppliercnpj'], 
        ':address' => $_POST['supplieraddress'],
        ':phone1' => $_POST['supplierphone1'],
        ':phone2' => $_POST['supplierphone2'],
        ':phone3' => $_POST['supplierphone3'],
        ':email1' => $_POST['supplieremail1'],
        ':email2' => $_POST['supplieremail2'],
        ':contact' => $_POST['suppliercontact'],
        ':paymenttype' => $_POST['supplierpaymenttype'],
        ':bank1' => $_POST['supplierbank1'],
        ':bank_agency1' => $_POST['supplierbankagency1'],
        ':bank_account1' => $_POST['supplierbankaccount1'],
        ':bank_accounttype' => $_POST['supplierbankaccounttype'],
    ); 
    try{ 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex){ 
        die("Failed to run query: " . $ex->getMessage()); 
    }
    //Informações armazenadas com sucesso
    echo 1;
    return true;
}else{
    //Nenhum dado informado
    echo 05;
    return true;
}

的.js

    $('#basic_validate').submit(function(e) {
    document.getElementById('supplierregistered').style.display = 'none';
    document.getElementById('supplierregistererror').style.display = 'none';
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "../suppliers/resources/controllers/register.php",
        async: true,
        data: $(this).serialize(),
        dataType : 'json',
        success: function(data) {
            if(data == 1){
                document.getElementById('supplierregistered').style.display = 'block';
                clearInputs();
                $('html, body').animate({ scrollTop: 0 }, 'fast');
            }else{
                document.getElementById('supplierregistererror').style.display = 'block';
                $('html, body').animate({ scrollTop: 0 }, 'fast');
            }
            alert(data); ////////Always return 1 even if name already exists in db... where it should return 01
        }
    });
});

function clearInputs(data){
    $("#basic_validate :input").each(function(){
        $(this).val('');
    });
};

即使db中已经存在名称,它也应该返回1 ...它应该返回01

0 个答案:

没有答案