I'm working on an existing script at the moment which uses Ajax, something I've never worked with before. I have a variable set in my javascript file which gets its value from an input field on my page. I need to use Ajax to post this to my PHP page only I've no idea where to start,

Im not sure what code you would need to see, but My javascript/AJAX code is, the variable I need to pass is 'var credoff'

 $(".getPoint").click(function()
 {        
   var theid = $(this).attr("id");
   var onlyID = theid.split("_");
   var onlyID = onlyID[1];
   var credoff = parseInt($(this).children('input.credoff:hidden').val());

 $.ajax({            
      url: 'do.php',
      type: 'POST',          
      data: "userID=" + onlyID,
      success: function(data) {
          if(data != "success1" && data != "success5") {
               $("#" + theid).text(data);  
          }else{

              $("#thediv_" + onlyID).fadeOut("slow");
              $('#creditsBalance').fadeOut("slow");
              newbalance = parseInt($('#creditsBalance').text());

Wouldit have to be in this format?

data: "userID=" + onlyID,
"credoff=" + credoff
share|improve this question

2 Answers

up vote 3 down vote accepted
...
data: {
    userId: onlyID,
    credoff: credoff
},
...
share|improve this answer
Ahhh that easy!, thanks silex – Liam May 25 '11 at 20:44
Np. Also look at this useful documentation: api.jquery.com/jQuery.ajax and devirtuoso.com/2009/07/… – silex May 25 '11 at 20:47
Sorry silex, would it have to be in the following format? data: "userID=" + onlyID, "credoff=" + credoff – Liam May 25 '11 at 20:47
Yes, it would. data: "userID=" + onlyID + "&credoff=" + credoff, – silex May 25 '11 at 20:50
It worked! Thanks so much – Liam May 25 '11 at 20:58

Or you can do this:

data: "userID=" + onlyID + "&credoff=" + credoff

don't forget the ampersand! &

share|improve this answer
It worked! Thanks so much – Liam May 25 '11 at 20:58

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.