php - Even after success, ajax success call executing else condition

I am returning response from php depending on if and else condition. Even after executing if condition, ajax success call executes else condition. Below is my code. Instead of getting transaction successful, I am getting don't have enough balance. I tried a lot of way but didn't work. Any help would be great.

php code

<?php
    if(isset($_POST['name'])){
        // some code
    }

    if(isset($_GET['paymentInfo']))
    {
        // some code
        if($dataPayerBalance >= $amount)
        {
            // some code
            $response = 'success'; 
        }
        else
        {
            $response = 'error'; 
        }
        // some code
        echo json_encode($response);
    }
?>

ajax call

 $.ajax({
            type: 'get',
            data: {'paymentInfo' : dataToSend},
            success: function(response)
            {
                if(response == 'success')
                {
                    $('.modal-body').html("Transaction Successful");
                    $('#empModal').modal('show');
                }
                else
                {
                    $('.modal-body').html("Don't have enough balance");
                    $('#empModal').modal('show');
                }
                            
            },
            error: function()
            {
                $('.modal-body').html("Error Occured");
                $('#empModal').modal('show');
            }
    });

Answer

Solution:

the problem is you are encoding a string as json and expecting not json, but string in the response. let's make them both jsons:

in your php code:

echo json_encode(['result'=>$response]);

in your ajax call:

$.ajax({
    url: "your api url",
    type: 'GET',
    data: {'paymentInfo' : dataToSend},
    dataType: 'json'
}).done(function( data ) {
    if (data.result == 'success'){
        //your custom action
    }
})
.fail(function() {
    //your custom action
});

Answer

Solution:

Try this!

PHP

 <?php

    if(isset($_POST['name'])){
        // some code
    }

    if(isset($_GET['paymentInfo']))
    {
        $response = array();
        // some code
        if($dataPayerBalance >= $amount) {
            // some code
           $response = ('status'=>'success');
        }
        else {
               $response = ('status'=>'error');
        }
        // some code
        echo json_encode($response);
    }
  ?> 

AJAX:

   $.ajax({
            type: 'get',
            data: {'paymentInfo' : dataToSend},
            dataType: 'json',
            success: function(response)
            {
               var jsonString = JSON.parse(response);

               if(jsonString.status == 'success')
                {
                    $('.modal-body').html("Transaction Successful");
                    $('#empModal').modal('show');
                }
                else
                {
                    $('.modal-body').html("Don't have enough balance");
                    $('#empModal').modal('show');
                }
                            
            },
            error: function()
            {
                $('.modal-body').html("Error Occured");
                $('#empModal').modal('show');
            }
    });

Source