Insert values into an INPUT type="number" or SELECT from the database

Asked

Viewed 489 times

0

Can I import database values into an input or select field using html, php, Mysql and/or bootstrap only? (Obs.: do not master Javascript or Json)

Follow image to illustrate, then I explain what I would like to do:

inserir a descrição da imagem aqui

In the ZIP code field, I would like you to take values already registered in the database and update the FU and the City as chosen, and that the initial page loading was blank.

If you do not have registration the idea would be to use the " + " to add a new one, but would not like to leave the page so as not to lose the data already entered.

I thought about using a modal to search and include ZIP code, but I don’t know how to leave the input "disabled" and then send values...

Does anyone have any idea what they might be doing to solve this?

Thanks in advance!!!

  • Could show some code, would help a lot, thank you :)

  • How do you make this query in the bank? Use some design architecture?

  • I appreciate your interest in solving, but I followed the advice of the jona1has and I decided using the API, so I don’t need to create the database, just import the data and if any fills directly, which is much more efficient by the fact that there can be more than one zip code per city and such....

1 answer

0


You’ll have to use some api for that, I’m using one on my system that uses viacep. I’ll send you a code as an example and you make the necessary changes.

It’s just so you get an idea of how it works.

$(document).ready(function($) {
$("#cep").blur(function() {

  //Nova vari�vel "cep" somente com d�gitos.
  var cep = $(this).val().replace(/\D/g, '');

  //Verifica se campo cep possui valor informado.
  if (cep != "") {

      //Express�o regular para validar o CEP.
      var validacep = /^[0-9]{8}$/;

      //Valida o formato do CEP.
      if(validacep.test(cep)) {

          //Preenche os campos com "..." enquanto consulta webservice.
          $('#street_address').val("...");
          $('#district').val("...");
          $('#number').val("...");
          $('#complement').val("...");

          //Consulta o webservice viacep.com.br/
          $.getJSON("https://viacep.com.br/ws/"+ cep +"/json/?callback=?", function(dados) {
            console.log(dados);
              if (!("erro" in dados)) {
                  //Atualiza os campos com os valores da consulta.
                  $("#street_address").val(dados.logradouro);
                  $("#district").val(dados.bairro);
                  $("#uf").val(dados.uf);
                  $("#locality").val(dados.localidade);
                  $("#number").val('');
                  $("#complement").val(dados.complemento);
              } //end if.
              else {
                  //CEP pesquisado n�o foi encontrado.
                  clean_up_form_doc();
                  alert("CEP n�o encontrado.");
              }
          });
      } //end if.
      else {
          //cep � inv�lido.
          clean_up_form_doc();
          alert("Formato de CEP inv�lido.");
      }
  } //end if.
  else {
      //cep sem valor, limpa formul�rio.
      clean_up_form_doc();
  }

}); });

  • Thank you! It helped a lot!

Browser other questions tagged

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