Wrong Return Javascript and PHP

Asked

Viewed 53 times

-2

Hello!

I am making use of a FIPE table API, using Javascript to get the data and passing to my database via PHP post. The point is that when passing to PHP, it is not sending the field name but the API code. Where am I wrong? Can you help me please?

  1. Javascript.
$(document).ready(function() {
    var urlBase = "//fipe.parallelum.com.br/api/v1/carros/marcas";

    /** Marcas**/

    $.getJSON(urlBase, function(data) {
      var items = ["<option value=\"\">ESCOLHA UMA MARCA</option>"];
      $.each(data, function(key, val) {
        items += ("<option value='" + val.codigo + "'>" + val.nome + "</option>");
      });
      $("#marcas").html(items);
    });

    /** Veiculo**/

    $("#marcas").change(function() {
      $.getJSON(urlBase + "/" + jQuery("#marcas").val() + "/" + "modelos", function(data) {
        var items = ["<option value=\"\">ESCOLHA UM VEICULO</option>"];
        $.each(data.modelos, function(key, val) {
          items += ("<option value='" + val.codigo + "'>" + val.nome + "</option>");
        });
        $("#modelos").html(items);
      });
    });

    /** Ano**/

    $("#modelos").change(function() {
      $.getJSON(urlBase + "/" + jQuery("#marcas").val() + "/" + "modelos" + "/" + jQuery("#modelos").val() + "/" + "anos", function(data) {
        var items = ["<option value=\"\">ESCOLHA O ANO</option>"];
        $.each(data, function(key, val) {
          //console.log(data)
          items += ("<option value='" + val.codigo + "'>" + val.nome + "</option>");
        });
        $("#ano").html(items);
      });
    });
  });

  1. register.php.
<div class="col-lg-3 form-group">
                                    <span>Marca</span>
                                    <div class="form-select">
                                        <select name="brand" id="marcas" class="form-control" style="height: 42px;">

                                        </select>
                                        <span class="select-icon"><i class="zmdi zmdi-chevron-down"></i></span>
                                    </div>
                                </div>
                                <div class="col-lg-3 form-group">
                                    <span>Modelo</span>
                                    <div class="form-select">
                                        <select name="model" id="modelos" class="form-control"
                                            style="height: 42px;">

                                        </select>
                                        <span class="select-icon"><i class="zmdi zmdi-chevron-down"></i></span>
                                    </div>
                                </div>
                                <div class="col-lg-2 form-group">
                                    <span>Ano</span>
                                    <div class="form-select">
                                        <select name="year" id="ano" class="form-control" style="height: 42px;">

                                        </select>
                                        <span class="select-icon"><i class="zmdi zmdi-chevron-down"></i></span>
                                    </div>
                                </div>
  1. post.php(Receiving the information).
$placa = test_input($_POST["placa"]);
$marca = test_input($_POST["brand"]);
$modelo = test_input($_POST["model"]);
$ano = test_input($_POST["year"]);
  1. On the / page at the base.

Na página / na base

inserir a descrição da imagem aqui

  • I honestly didn’t understand your problem. I could illustrate it better?

  • @Clear War... In item 4, on my registration page, in the tag field for example, I get my javascript return as text (My brand name), but when I go to my PHP that will send it to the database, it passes the "Code" of the brand and not the "Name" that is in the field, as shown in the last image of the database showing only the code.

2 answers

0


I managed to solve!

I put this Function to call the contents of the option.

<script>
    $(document).ready(function() {
        var marcas = $("#marcas");
        var modelos = $("#modelos");

        var marcaSelecao = $("#marcaSelecao");
        var modeloSelecao = $("#modeloSelecao");

        marcas.change(function(){
            marcaSelecao.val('');
            modeloSelecao.val('');

            var valor = $("#marcas option:selected").html();
            marcaSelecao.val(valor);
        });

        modelos.change(function(){
            var valor = $("#modelos option:selected").html();
            modeloSelecao.val(valor);
        });
    });
</script>

0

/** Marcas**/

    $.getJSON(urlBase, function(data) {
      var items = ["<option value=\"\">ESCOLHA UMA MARCA</option>"];
      $.each(data, function(key, val) {
        items += ("<option value='" + val.codigo + "'>" + val.nome + "</option>");
      });
      $("#marcas").html(items);
    });

subistitua by

/** Markings**/

$.getJSON(urlBase, function(data) {
  var items = ["<option value=\"\">ESCOLHA UMA MARCA</option>"];
  $.each(data, function(key, val) {
    items += ("<option value='" + val.nome + "'>" + val.nome + "</option>");
  });
  $("#marcas").html(items);
});

you can replace in BRANDS/TEMPLATES and YEAR, that your field will receive the text values, and no longer the Ids. That way you don’t need this function to take Selected and replace when sending....

Browser other questions tagged

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