How to update an id with Ajax

0

Hello people I have a problem with id I can not update an ID in case there is a message without seeing as well as facebook

I want the user id to be updated but it does not work. I hope friends help me

HTML:

<div id="'.$fila['userID'].'" class="Pending_messages_user"></div> 

JS:

function actualizar(){

    var user_id = $(this).attr("id");
    var datauser = 'user_id='+ user_id ;

    $.ajax({
        type: 'POST',
        url: 'codigo.php',
        data: datauser,
        success: function(respuesta) {

        //Pegamos el contenido de update2.php en la div id#Pending_messages_user
        $('.Pending_messages_user').html(respuesta);
   }
});
}

setInterval( function(){
    actualizar();   
},1000)//Actualizamos cada 1 segundo

    
asked by Shareiv 22.07.2017 в 05:44
source

1 answer

1

What happens is very simple ... Your ajax is sending the id and in your PHP controller you change the variable. But to see the change in your HTML you have to "refresh" the page to change the IDs using the php variable as you do ... What you can do is use JS to inject the new IDs that you receive from AJAX.

funcion success(response){
  for(int i = 0, i < response.length; i++){
      $("div[id="+response[i].anterior+"]).attr("id",response.nuevo);
  }
}

Something like that I imagine myself ... So you change the IDs dynamically. You can not change them directly with PHP, remember that when you load the page, that $ row ['userID'] is painted but once it is painted it is static, not dynamic.

    
answered by 22.07.2017 / 16:39
source