-3

I am having a problem with my code as my php variable is not being echoed on page when using ajax jquery. Here is my code...

<script>
    function refresh_div() {
     $('.loader').show();
     var username = "<?php echo $user; ?>";
     jQuery.ajax({
            type: "POST",
            url: "load.php",
            data:{user : username},
            success:function() {
                jQuery("#load_msgs").append(response+'<br>');

            },
            complete: function(){
        $('.loader').hide();
      }
        });
    }

    t = setInterval(refresh_div,1000);
</script>

i am trying to send "username" to page url "load.php"... i called it this way but nothing is being echoed...

if(isset($_POST['user'])) {
     echo $_POST['user'];
}

pls help out thanks... :)

edited...

when i tried using this code i.e adding passing response as parameter in success function like this ...

<script>
    function refresh_div(response) {
     $('.loader').show();
     var username = "<?php echo $user; ?>";
     jQuery.ajax({
            type: "POST",
            url: "load.php",
            data:{user : username},
            success:function() {
                jQuery("#load_msgs").append(response+'<br>');

            },
            complete: function(){
        $('.loader').hide();
      }
        });
    }

    t = setInterval(refresh_div,1000);
</script>

.... the data (username) gets displayed every second.. like a blink toggling on and off the page... how do i make the data display static in order to use the variable on the ajax page. Thanks :)

18
  • 1
    Just pass response in success function success:function(response) { Commented Dec 1, 2015 at 13:05
  • 1
    You appear to already have asked this question: stackoverflow.com/questions/34014264/…. If the solutions provided there did not work, please comment on those answers and edit your question to include more detail. Commented Dec 1, 2015 at 13:06
  • 1
    @PraveenKumar ?? What? the text in the questions match 100% Commented Dec 1, 2015 at 13:07
  • 1
    @adetiwa - SO is not a "Fix my code ASAP". Commented Dec 1, 2015 at 13:09
  • 1
    The 1st comment answers your question. Why havent you tried that? Also the second code block in your question (the bit after edited...) is identical to the 1st Commented Dec 1, 2015 at 13:12

1 Answer 1

1

You are not defining response anywhere. Send it as the parameter of the success handler:

success: function(response) {
    jQuery("#load_msgs").append(response+'<br>');
},
Sign up to request clarification or add additional context in comments.

8 Comments

I tried defining response like you suggested before , it displayed but kept toggling on and off every second like a blink.... do you have any idea why it is doing that?
@adetiwa You aware you are using an interval, right?!...
@adetiwa It is because you are using setInterval().
@A. Wolff yes i am... to reload messages every second asynchronously
@adetiwa Awesome, and then why don't you expect it to 'blink'?!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.