0

I'm using a PHP script that is waiting for two values through $_GET.

I'm trying to pass those 2 values using jQuery and that's where I'm not too good at.

Here's the code I've got.

Can somebody point me in the right direction ? Thanks !

function xrate(id,rating){  

   $.ajax({
      url: "ajax_xrate.php?id="+id+"&rate="+rating,
      global: false,
      type: "GET",
      dataType: "text",
      async:false,
      success: function(){
         alert('Bravo!');
      }
   });

}

(actual code copied from the comments)

function xrate(id,rating){ 
    var oDate = new Date(); 
    $.ajaxSetup({ cache: false }); 
    $.ajaxSetup({ 
        scriptCharset: "utf-8" ,
        contentType: "application/x-www-form-urlencoded; charset=UTF-8" 
    }); 
    $.ajax({
        url: 'ajax_xrate.php?timestamp='+oDate.getMilliseconds(), 
        dataType: 'html', 
        data: {itemid:id, keyrate:rating}, 
        cache: false, 
        type: "GET", 
        success : function(dataReturn){ 
            alert(dataReturn); 
        } 
    }); 
}
4
  • api.jquery.com/jQuery.ajax Commented Mar 2, 2011 at 18:34
  • And what exactly is happening? Do you have a problem? My guess would be problems with parameter encoding (and async:false is a bad idea). Commented Mar 2, 2011 at 18:39
  • My code is actually this : function xrate(id,rating){ var oDate = new Date(); $.ajaxSetup({ cache: false }); $.ajaxSetup({ scriptCharset: "utf-8" ,contentType: "application/x-www-form-urlencoded; charset=UTF-8" }); $.ajax({ url: 'ajax_xrate.php?timestamp='+oDate.getMilliseconds(), dataType: 'html', data: {itemid:id,keyrate:rating}, cache: false, type: "GET", success : function(dataReturn){ alert(dataReturn); } }); } On this other side, I'm echoing the values with $print_r($_GET); But I don't get anything echoed ... Commented Mar 2, 2011 at 20:11
  • Maybe because it should print_r($_GET) (without $). Commented Mar 2, 2011 at 22:38

4 Answers 4

2
function xrate(id,rating){  

   $.ajax({
      url: "ajax_xrate.php",
      data: {
          id: id,
          rate:rating
      },
      type: "GET",
      success: function(){
         alert('Bravo!');
      }
   });

}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi hsz, thanks for your quick answer !! Although I'm pretty sure your answer is correct, I can't seem to be able to make it work. How should I get the values in the PHP script ? $id_value = $_GET['id']; $rate_value = $GET['rate']; Would it be correct ? Also, how can I use "echos" in my PHP script to help be debug, and see the results appear in the webpage ?
@user641747: To get the values is correct that you mentioned. To debug in webpage, although I don't recommend it. You use success: function(text){ // use the text parameter to see any reporting }
1

you can do:

function xrate(id,rating){  

   $.get("ajax_xrate.php",
      {
          'id': id,
          'rate': rating
      }, 
     function(){
       alert('Bravo!')
     }
  );

}

Comments

0
jquery.ajax work like this

jQuery.ajax({
   url:'you_ur',
   type:'GET'  or 'POST',
   data:{prop1:value1},  // name value pair of props waiting on server
   //etc etc here
});

http://api.jquery.com/jQuery.ajax/

Comments

0
function xrate(id,rating){  

   $.ajax({
          url: "ajax_xrate.php",
          data: "id="+id+"&rate="+rating,
          async:false,
          success: function(){
             alert('Bravo!');
          }
   });

}

You don't need set parameters like type = 'get' because they are already default. Every parameters shall be inputed in the data: either in string form(like the one above) or object form({id: '45', rate: 'something'}). Async is also true on default in case you didn't know

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.