tags:

views:

118

answers:

2

Good morning all - new to Jquery $.get and $.ajax and your help is appreciated.

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){

});

Thanks in advance.

Bob Knothe

+3  A: 

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"]);
 ?>
Victor
Gents,Thanks I will give it a try and get back to youBob
Bob Knothe
+1  A: 

try

var getRate = $.get('exchangeRate.php', {param1:"val", param2:"val2"}, function(data){
});

or

var getRate = $.get('exchangeRate.php?param1=val&param2=val2', function(data){
});
Chris Gutierrez
I don't think he wants getRate to be the XMLHttpRequest object returned by `$.get`...
Victor
You are right. I guess that is what I get for copying a pasting from the original source. Good catch.
Chris Gutierrez
Trying your suggestions will get back to you hopefully with success
Bob Knothe
success! ThanksI was missing the echo exchangeRate($_GET["exchangeFrom"], $_GET["exchangeTo"]); line in the PHP script.Thanks againBob
Bob Knothe