JQuery $.get or $.ajax call PHP script on server that requires 2 parameters

ajaxjquery

I have created a PHP script functionexchangeRate($exchangeFrom, $exchangeTo) that uses uses two parameters.

I am trying to call this PHP script with Jquery's $.get function but I can not figure out how to send the two parameters (I feel like a turkey – pun intended).

var getRate = $.get('exchangeRate.php', function(data){

});

Best Answer

you have to use a callback, or call it synchronously:

$.get("exchangeRate.php", {exchangeFrom:"what",exchangeTo:"ever"},function(resp){
    alert(resp);
    //resp is what your page returns!
    //find getRate in resp and use it here
});

to make things synchronous you need something like

$.ajaxSetup({
    async: false,
});
var getRate = null;
$.get("exchangeRate.php", {exchangeFrom:"what",exchangeTo:"ever"},function(resp){
    alert(resp);
    //resp is what your page returns!
    //find getRate in resp
    getRate = something;
});
//use getRate here

Also, I guess your PHP is right, with something like

 <?php 
   function exchangeRate($exchangeFrom, $exchangeTo){...}
   echo exchangeRate($_GET["exchangeFrom"], $_GET["exchangeTo"]);
 ?>