0

I have the following jQuery code with an Ajax function

$(document).ready(function(){
    $(".ver").click(function(event){
    var pulsado = $(this).data("dnipass"); //pulsado is "12345678B" (a string) taken from php code
    alert(pulsado);
    event.preventDefault();

    $.ajax({
        type: 'POST',
        url: 'adminVerLineas.php',
        data: {
            dni:$(this).data("dnipass"),
        },
        success:function(data){
            alert(data); //Data is string(9) "123456789" , i can see the alert
            $(this).closest('.form-group').next('.userInfo').append(data); //this doesnt append properly, but i want this to append
        }

    });    

    $(this).closest('.form-group').next('.userInfo').append(pulsado); //this appends properly
});
})

Problem is that the append inside de success functions isnt working, but the one outside does, i dont understand why is that happening

This is where im doing the append:

 <div id ="<?= $i?>" class="table userInfo" data-formpost="<?= $dni?>"></div>

This is my adminVerLineas.php in case its needed:

<?php
var_dump($_POST["dni"]);
?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mouchin777
  • 1,428
  • 1
  • 31
  • 59
  • It might be ajax call is failing . Are you getting alert? – Mahesh Nov 21 '19 at 11:49
  • @TatvasoftCorp yes the alert is being displayed with my desired data – mouchin777 Nov 21 '19 at 11:50
  • 2
    the thing is that your $(this) inside the success function is not in the correct scope. you should declare something like var _self = this; (above the ajax call) and inside the success function use $(_self) to get to the relevant scope – Max Svidlo Nov 21 '19 at 11:51
  • 1
    $('.ver').closest('.form-group').next('.userInfo').append(data); replace this line – Mahesh Nov 21 '19 at 11:54

0 Answers0