PHP variable not echoing on page via ajax jquery

后端 未结 4 477
温柔的废话
温柔的废话 2020-12-22 13:16

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...



        
相关标签:
4条回答
  • 2020-12-22 13:43

    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);
    
    }
    
    0 讨论(0)
  • 2020-12-22 13:44

    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.

    0 讨论(0)
  • 2020-12-22 13:50

    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>
    
    0 讨论(0)
  • 2020-12-22 14:02

    Where the response variable you get? Should be :

    success : function( response ) {
       jQuery("#load_msgs").append(response+'<br>');
    
    },
    
    0 讨论(0)
提交回复
热议问题