-1

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() {
     $('.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 :)

6
  • Check the request headers & from data on network tab. Commented Dec 1, 2015 at 6:51
  • change success:function() { to success:function(response) { also you did not specify dataType so you need to parse Commented Dec 1, 2015 at 6:52
  • when i changed it to success:function(response) { , it kept toggling on and off very frequently(every second probably)... i already tried that Commented Dec 1, 2015 at 6:56
  • can you add dataType:text and try Commented Dec 1, 2015 at 6:57
  • where did you wrote your JS code ? in same PHP file ? if code is written in php file, then it should echo the var. Commented Dec 1, 2015 at 6:59

4 Answers 4

0

Where the response variable you get? Should be :

success : function( response ) {
   jQuery("#load_msgs").append(response+'<br>');

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

5 Comments

i did this and it started echoing data and disappearing every second.. like a blink... what can i do to stop the toggling?
Does the data append inside #load_msgs container?
yes... then it starts toggling off and on.. every second
What is $('.loader')?
loader is the class of the spinning img gif i used
0

please take a datatype as json in ajax call and in loads.php function add two line as below..

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

also change in success function.

success:function(response) {
                jQuery("#load_msgs").append(response.user);

}

Comments

0

A bit more simplified would be:

$.post(URL, { data: somefield }, function(result) {
    result = JSON.parse(result);
    If(result && result.status === 'true'){

    }
)};

Depends a bit on the return at server side.

Comments

0

You missed response in success function. Try this:

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

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

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

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.