tags:

views:

87

answers:

2

Hey guys quick question, I am sending a request with the jquery ajax function with a variable. After the request I want that variable updated to match the value of the sent back information, so next time the ajax function executes, it send the value of the new variable.
EDIT

$(document).ready(function()
{
var countusers='<?php echo $countusers; ?>';

function refresh() {

 $.ajax({
   type: "POST",
   data: "action=refresh_topic&countusers=" + countusers,
   url: "tester.php",
    dataType: 'json',
   success: function(json)
   {
    var countusers=json.countusers;
 }
})
}
setInterval(refresh, 3000);

    });
+5  A: 

Your defining the variable inside your success function. Define it outside so it has a global scope then update it inside your success function.

var countusers = 0;
$.ajax({
   type: "POST",
   data: {"countusers": countusers},
   url: "tester.php",
   dataType: 'json',
   success: function(json){
      countusers=json.rownumber;
   }
});
PetersenDidIt
I already had that but for some reason it just wasn't working. It must be something else like a wrong value being sent if you state that hypothetically it should work.
Scarface
Your updated code still has it redefining countusers in the success callback. Remove the var infront of countusers in the callback and it should work
PetersenDidIt
+1  A: 

the responce below is correct!

just a notation, since you are using countusers also before it change, it assume that you are setting it also before you call AJAX the first time!

UPDATE:

<head>
<script>
$(function() {

function refresh() {

var countusers = $('#count_my_users').text();

 $.ajax({
   type: "POST",
   data: "action=refresh_topic&countusers=" + countusers,
   url: "tester.php",
   dataType: 'json',
   success: function(json)
   {
    $('#count_my_users').empty().append( json.countusers );
   // var countusers=json.countusers;
 }
})
}

setInterval(refresh, 3000);

    });
</script>
</head>

<body>
<div id="count_my_users"><?php echo $countusers; ?></div>
          <!--/ THIS DIV HOLD USER COUNT /-->
</body>
aSeptik
Sorry but I do not quite understand your second check, can you briefly explain to me what the significance of it is. Sorry but I am quite new.
Scarface
What is this.id?
Scarface
this.id is supposed to be the id of the click, but is just an example of pseudo value, in your case it should be countusers = your_value
aSeptik
I noticed that while the countusers variable is defined within the success as the new value, outside it is defined as its original value. How can I change this, by this id do you mean I have to write the variable to a div?
Scarface
PS once again, sorry if I am not quite getting it, I am just really noob lol, I have to sit down and read up on jquery.
Scarface
When I figure this out, I will change the answer to yours.
Scarface
can you provide the full js part of your code!?
aSeptik
see the updates!
aSeptik
I see what you are saying now, so if the variable is undefined you set it equal to the id. My variable however is defined no matter what. It will be a number from 1-however-many-users there are. Initially it is defined by a mysql query and echoed in php, then when the function is executed it sends data back. The problem is, the new definition of countusers is only defined within the success function. I am going to edit my initial post so you can see all the code.
Scarface
see updates! and let me know!
aSeptik
Thanks for sticking with me man, I gave you as much points as I could.
Scarface
you are welcome! bro! ;-)
aSeptik