Popular type["text"] by select

Asked

Viewed 245 times

0

I’m trying to popular a type["text"] through jquery and it’s not working!

Form:

  <div>
        <label>Plano:</label>
        <select name="plano" id="plano" required>
             <?php echo $stringPlanos; ?>
        </select> <br /><br />
  </div>

  <div style="float:left">    
   <label class="labelPequeno">Valor</label><input type="text" class="real" style="width:100px; height:40px" required maxlength="23" id="valorCombinado"  name="valorCombinado" /> <br /><br />
  </div>    

Jquery code

  <!-- INICIO ENTREGA VALOR DO PLANO -->
  <script src="http://www.google.com/jsapi"></script>

  <script type="text/javascript">
  $(function(){
      $('#plano').change(function(){
          if( $(this).val() ) {
              $.getJSON('planos.ajax.php?search=',{cod_plano: $(this).val(), ajax: 'true'}, function(resultado){
                  var resultado = planosRetorno;
                  alert(resultado);
                  $('#valorCombinado').val(resultado).show();
              });
          } else {
              $('#valorCombinado').val('0.00');
          }
      });           
  });         
  </script>
  <!-- FIM ENTREGA VALOR DO PLANO -->

ajax.php plans

<?php
 $cod_plano = mysql_real_escape_string( $_REQUEST['cod_plano'] );
 $planosBase = $PlanosDao->pesquisaPlanoEdicao($cod_plano);
 $planosRetorno = $planosBase->getValor();
 echo( json_encode($planosRetorno) );
?>

But, the browser console shows no$ errors and the field is not populated!

Where am I going wrong?

  • If what you want popular is a input you have to use .val(resultado) and not .html(resultado). Is that the problem?

  • $('#valueCombined'). val(result). show();??? Didn’t work. But I guess not. Because even Alert doesn’t work!

  • Test this code: http://jsfiddle.net/e3jdemdb/

  • Don’t go changing the question please... leave it as it was, even if it was wrong. It may be useful for others.

  • Sérgio. It didn’t work. Neither did Alert work! But remember I’m using the jquery autocomplete plugin.Want me to add his call to the question?

  • Usa echo json_encode($planosRetorno); unrelated here: echo().

  • When you use this code I put in jsFiddle what gives you in ajax headers?

  • not so Sérgio. headers from Ajax? How do I see this?

  • What happens is that these select values are changed not in select, but in a dynamically created lis UL. In that case, I will have to figure out how to get the value of the selected li! Will have to?

Show 5 more comments

1 answer

1

Whoa buddy, you’re capturing the wrong variable in this code snippet:

          ...
          $.getJSON('planos.ajax.php?search=',{cod_plano: $(this).val(), ajax: 'true'}, function(resultado){
              var resultado = planosRetorno;
              alert(resultado);
              $('#valorCombinado').val(resultado).show();
          });
          ...

The variable outworking is capturing a wrong value: planosReturn does not exist, the return is actually already outworking

If you are having trouble checking the error of your request do a test using the ajax method of jquery:

$.ajax( "example.php")
    .done(function() {
        alert( "success" );
    })
    .fail(function() {
        alert( "error" );
    })
    .always(function() {
        alert( "complete" );
    });

(http://api.jquery.com/jquery.ajax/)

  • Ok. But echo( json_encode($plansRevise) ); doesn’t create a js variable of the same name? Or isn’t that how you interpret?

  • in fact the idea is as follows: $.getJSON([url][parametro via get para a url][options], function([retorno da url]){ [função]});

Browser other questions tagged

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