Value collection via PHP and Json

Asked

Viewed 51 times

0

I have a select where the data is in a JSON database that are available in the link:

https://gist.github.com/letanure/3012978.jsinserir a descrição da imagem aqui

The select option works normally , that is, the data appears in the designated field but I can’t rescue the value and save in the database. When sending the values are saved as 0. Other fields sent in the form are saved normally. Does anyone know how I can collect the value provided via JSON and save to the database ? To save the data I am using PHP .

Part HTML

        <div class="form-row ">
            <div class="col-md-3 col-sm-3  mb-3 ">
                <label >Estado </label>
                   <Select class="form-control" name="Estado" ID="Estado"  required>
                      <option selected value=""></option>
                  </Select>
            </div>
            <div class="col-md-3 col-sm-3 mb-3 ">
                <label >Cidade</label>
                <Select class="form-control" name="Cidade" ID="Cidade"  required>
                      <option selected value=""></option>
                  </Select>
            </div>

Javascript

$(document).ready(function(){
    carregar_json('Estado');
    function carregar_json(id, cidade_id){
        var html = '';

        $.getJSON('https://gist.githubusercontent.com/letanure/3012978/raw/36fc21d9e2fc45c078e0e0e07cce3c81965db8f9/estados-cidades.json', function(data){
            html += '<option>Selecionar '+ id +'</option>';
            console.log(data);
            if(id == 'Estado' && cidade_id == null){
                for(var i = 0; i < data.estados.length; i++){
                    html += '<option value='+ data.estados[i].sigla +'>'+ data.estados[i].nome+'</option>';

                }
            }else{
                for(var i = 0; i < data.estados.length; i++){
                    if(data.estados[i].sigla == cidade_id){
                        for(var j = 0; j < data.estados[i].cidades.length; j++){
                            html += '<option value='+ data.estados[i].sigla +'>'+data.estados[i].cidades[j]+ '</option>';

                        }
                    }
                }
            }

            $('#'+id).html(html);

        });

    }

    $(document).on('change', '#Estado', function(){
        var cidade_id = $(this).val();
        console.log(cidade_id);
        if(cidade_id != null){
            carregar_json('Cidade', cidade_id);
        }
    });

});
  • The Json file contains a list of cities and states of Brazil in the following format: { "states": [ { "acronym": "AC", "name": "Acre", "cities": [ "Acropolis", "Assis Brasil", Etc .. Link: https://gist.github.com/letanure/3012978

  • It seems that the dynamically generated value is missing between quotation marks (") html += '<option value="'+ data.estados[i].sigla +'">'.

  • At first the only problem I found is that the values of the cities are with the uf of the state, this should be changed to the name of the city or IBGE code. About the insertion in the database, add the code that makes this sending the data

  • It follows part of the code . I believe that this is not the issue because all the data forwarded are recorded . if(isset($_POST['Sign Up_user'])){ $Status= $_POST['Status']; $City= $_POST['City']; $sql ="INSERT INTO table (State,City VALUES'$State','$$City'')";

  • Benilson , I made the changes but the result is still sent blank to the bank

No answers

Browser other questions tagged

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