user986959
user986959

Reputation: 630

PHP no output from jquery function

I'm trying to get data back from the database but I cannot figure out why I'm not getting any output. I'm using jQuery/AJAX:

$(document).ready(function(){
      $('#banktransfer_blz').blur( function(){
                    send_blz( $(this).val() );
            } );

    } );

function send_blz(str){
      $.post( "get_swift.php", 
          { sendBLZ: str }, 
          function(data){
            $('#banktransfer_bic').val( data.return_bic ).html();
          },
          "json");
    }

And here is the get_swift.php:

if (isset($_POST['sendBLZ'])){
$value = $_POST['sendBLZ']; 
}

$test = "test";

$swift = xtc_db_query("SELECT customers_id FROM customers WHERE customers_cid ='20002'"); 


echo json_encode( array( "return_bic" => $swift) ); 

I am connected to the database.

Upvotes: 0

Views: 58

Answers (3)

Krish R
Krish R

Reputation: 22721

Try this, You need to use xtc_db_fetch_array to fetch the customers_id from table

in get_swift.php:

 $swift = xtc_db_query("SELECT customers_id FROM customers WHERE customers_cid ='20002'");
 $row = xtc_db_fetch_array($swift);
 echo json_encode( array( "return_bic" => $row['customers_id']) );

Also,

 $('#banktransfer_bic').val( data.return_bic );

instead of

$('#banktransfer_bic').val( data.return_bic ).html();

Upvotes: 2

Mutale
Mutale

Reputation: 310

I am assuming you get the desired response when you calling the php file directly.

You are resetting the #banktransfer_bic html by doing $('#banktransfer_bic').html();

try to use: $('#banktransfer_bic').html(data.return_bic); instead

Upvotes: 0

Try using Browser's developer tools and check XHR request. If it's ok, make sure the page from where you are going to fetch data is separately connected to database. You can check this by visiting this page directly from browser's URL and the output should be what you want through ajax.

also in your ajax code, try changing

$('#banktransfer_bic').val( data.return_bic ).html();

to

$('#banktransfer_bic').html( data.return_bic );

Upvotes: 0

Related Questions