tags:

views:

16

answers:

1

Can anyone better experienced then me, spot the issue here. I'm unable to debug it, my var_dumps don't seem to get any effect due to the use of jquery.post() so I believe.

I get nothing displayed. I was expecting to receive a li series filled with json encoded values.

The HTML:

<div id='data'></div>
<form action="">
  <input id="nomeInput" type="text" name="nomeInput" value="" autocomplete="false"/>
</form>
<ul id="listaDominios" style="display: none;">
</ul>

The js:

$(document).ready(function(){
  $('#nomeInput').keypress(function(){
     $.post("testeBasico_1.php", {nomeInput : $('#nomeInput').val()}, function(resposta) {
        for (var x = 0, tamanhoDados = resposta.nomeDominio.length; x < tamanhoDados; x++){
           $('#listaDominios').show();
           $('#listaDominios').append('<li>'+resposta.nomeDominio[x]+'</li>');
        }

      }, "json");

  });//end of keypress;

});//end of document ready;

The PHP

public function listaDominios(DominioVo $dominioVo)
{
  try
  {
     $stmt = $this->_dbh->prepare("SELECT d.nomeDominio FROM dominio d WHERE d.nomeDominio LIKE ?");
     $stmt->bindValue(1,'%' . 'a' . '%', PDO::PARAM_STR);
     $stmt->execute();

     $resultado = $stmt->fetchAll(PDO::FETCH_OBJ);

       return $resultado;
    }
    catch (PDOException $ex)
    {
      echo "Erro: " . $ex->getMessage();
    }
}

If the spot gets to be a difficult catch, how can I spot it. It's my first ajax experience, so I'm not comfortable with the debugging part. :s

Suspicion: (UPDATE) I believe the issue is in the way I'm trying to iterate over the returned json. Here's the echo format of the json_encoded:

[{"nomeDominio":"aaaa.ka"},{"nomeDominio":"agentesdeexecucao.ka"}]

Thanks a lot in advance, MEM

+1  A: 

Since the base of the object is an array you need to iterate over it at the root level, so your for loop should look like this:

$('#listaDominios').toggle(resposta.length > 0);
for (var x = 0; x < resposta.length; x++){
  $('#listaDominios').append('<li>'+resposta[x].nomeDominio+'</li>');
}

Or the $.each() route:

$('#listaDominios').toggle(resposta.length > 0);
$.each(resposta, function() {
  $('<li />', { text: this.nomeDominio }).appendTo('#listaDominios');
});

The important part is that resposta.nomeDominio isn't anything, since the root of the response is an Array, however resposta.length will get the length, so use that. Also since the array is at the root and each object in it has a nomeDominio property, you want resposta[x].nomeDominio to go to the current index (to get the object), then call .nomeDominio to get the property. Or, use the $.each() route in which this refers to the current object, either way works.

Nick Craver
@Nick Craver: Despite your nice explanation, I still have doubts that I hope you or anyone else can clear out. Why $('#listaDominios').toggle(resposta.length > 0); ?+resposta[x].nomeDominio+ - Why resposta[x] ? I mean, I get that that X will be 1, 2 3 etc... right? But, how did you know from this: "[{"nomeDominio":"aaaa.ka"},{"nomeDominio":"agentesdeexecucao.ka"}]" that THAT will be the way to access each different "nomeDominio" key ?Thanks in advance,MEM
MEM
@MEM - I'm using `.toggle()` because it's a quick `.show()` or `.hide()` based on if there are any elements. Also you use `resposta[x]` because `resposta` is the returned object, with is an Array...this is how your access an array :)
Nick Craver
About the second question: Is it because, *naturally* the array as numeric key indexes? I thought the key on each of those key/value pars were "nomeDominio" and that, no number was associated to them...
MEM
@Nick Craver: So, if we have this json string: [{"nomeDominio":"aaaa.ka"},{"nomeDominio":"agentesdeexecucao.ka"}]We have 1 ARRAY (noted by the []) that contains 2 OBJECTS noted by the {}, so what we are doing is iterate over an array of objects. And, each object of that array is an element of that array, so, in order to access those, we use a numerical reference. Precise?
MEM
@MEM - Correct, and once you have that object, via `array[index]` , you're just getting a property off of it.
Nick Craver
Yeah! Nice! Thanks a lot! :) A very important lesson I learned today. I start to like arrays more. :D Again, thanks for your time and availability.
MEM
Ups... one last question:Will not the each evaluate the length each time and, hence, be slower than if we do:for (var x = 0, sizeData = resposta.length; x < sizeData; x++){//we get the sizeData on the first iteration only, from there, always the same value, hence, faster way to access?
MEM