PHP + Jquery + JSON

Asked

Viewed 674 times

2

Have function calling an ajax. As follows below:

    function verifica(){ 
      var meuid = $('.meuid').attr('id');   
      var datas = "user="+meuid;        
      $.ajax({
             type: "GET",
             url: 'sys/stream2.php',                                  
             data: datas                
          }).done(function( data ) {                                         
            //alert(data);
            $('#nome').html(data);                
          });
    }

And on the stream, I have a select for that along with a foreach as follows:

foreach ($gUsuarios as $usuarios) {
        $agora = $usuarios['AGORA'];   
        if ($agora >= $usuarios['cUsu_Limite']) {            
            echo json_encode(array('usuarioon' => $usuarios['cUsu_ID'],
                                   'status' => 'fa fa-circle-o text-red'));            
        }else{    
            echo json_encode(array('usuarioon' => $usuarios['cUsu_ID'],
                                       'status' => 'fa fa-circle-o text-green'));
        }

and the return, presents me like this:

{"usuarioon":"1","status":"fa fa-circle-o text-red"}
{"usuarioon":"3","status":"fa fa-circle-o text-red"}    

That means it returns me correctly, but the problem is: how to split it individually? I’ve tried with ParseJson, but it didn’t work, I think it’s because I’m returning a array, within a foreach. What I really want is:

in the element it changes the class to: 'fa fa-Circle-o text-green', but for this I need to know which user, so I bring the user code attached. I need to set like this:

$('#'+data.usuarioon). addClass(date.status);

Someone’s been there, you can help me ??

  • I think the title of the question should be more specific, no?

  • And what do you think the title @Wallacemaxters should look like? From what I see the title would be simply to identify my problem, so there is the content... Anything I can add to clear my doubt ?

  • It’s right that the content is in the question, so that’s why you need a good title. Because other people who go through your problem will be able to search and find.

  • Bruno just as a suggestion, the title should summarize the problem as short as possible, so anyone who sees your question, already knows the problem, and will read your description for more details on how it helps you solve. Don’t take it personally, @Wallacemaxters' suggestion was to help you improve the visibility of the question on the site :)

1 answer

0


This problem is the same as jquery ui autocomplete + php does not work

You are generating more than one JSON.

Change the PHP:

Change this:

foreach ($gUsuarios as $usuarios) {
        $agora = $usuarios['AGORA'];   
        if ($agora >= $usuarios['cUsu_Limite']) {            
            echo json_encode(array('usuarioon' => $usuarios['cUsu_ID'],
                                   'status' => 'fa fa-circle-o text-red'));            
        }else{    
            echo json_encode(array('usuarioon' => $usuarios['cUsu_ID'],
                                       'status' => 'fa fa-circle-o text-green'));
        }
}

That’s why:

$jsonAux = array(); 
// Algumas versões do PHP (acho que < PHP 7) exibem no erro se não houver isso!

foreach ($gUsuarios as $usuarios) {
        $agora = $usuarios['AGORA'];   
        if ($agora >= $usuarios['cUsu_Limite']) {            
            $jsonAux[] = array('usuarioon' => $usuarios['cUsu_ID'],
                                   'status' => 'fa fa-circle-o text-red');            
        }else{    
            $jsonAux[] = array('usuarioon' => $usuarios['cUsu_ID'],
                                       'status' => 'fa fa-circle-o text-green');
        }
}

echo json_encode($jsonAux);

Upshot:

[{"usuarioon":"1","status":"fa fa-circle-o text-red"},{"usuarioon":"3","status":"fa fa-circle-o text-red"}]

Meanwhile in Jquery:

You need to create a loop to run each array defined above, so it will run each time.

function verifica(){ 
  var meuid = $('.meuid').attr('id');   
  var datas = "user="+meuid;        
  $.ajax({
         type: "GET",
         url: 'sys/stream2.php',                                  
         data: datas                
      }).done(function( data ) {                                         

         // Cria um loop para cada array do data:
         $.each(data, function(a) {
            $('#'+this.usuarioon).addClass(this.status);         
         }


      });
}

Test me:

For reasons of viewing the addClass was changed to text!

var data = [{
  "usuarioon": "1",
  "status": "fa fa-circle-o text-red"
}, {
  "usuarioon": "3",
  "status": "fa fa-circle-o text-red"
}];


$(':button').click(function() {

  $.each(data, function(a) {
    $('#' + this.usuarioon).text(this.status);
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<x id='0'>Meu é ID 0</x>
<br>
<x id='1'>Meu é ID 1</x>
<br>
<x id='2'>Meu é ID 2</x>
<br>
<x id='3'>Meu é ID 3</x>
<br>
<x id='4'>Meu é ID 4</x>
<br>

<button>SIMULAR AJAX</button>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.