Keep selected edit status

Asked

Viewed 46 times

0

Colleagues.

I have a registration form from which the user when choosing the state, automatically appears the city. But I would like in the edition, the city already appeared selected. The code I have is this:

<div class="item form-group">
                      <label class="control-label col-md-3 col-sm-3 col-xs-12" for="">Estado: <span class="required">*</span>
                      </label>
                      <div class="col-md-6 col-sm-6 col-xs-12">
                        <?php echo $metodos->listarEstados($visualizar->Estados); ?>
                      </div>
                    </div>                   
                      <div class="item form-group">
                      <label class="control-label col-md-3 col-sm-3 col-xs-12" for="">Cidade: <span class="required">*</span>
                      </label>
                      <div class="col-md-6 col-sm-6 col-xs-12">
                        <!--<span class="carregando">Aguarde, carregando...</span>-->
                        <select name="Cidades" class="select2_single form-control" id="cidades">              
                </select>
</div>

Jquery

$(function(){
    $("#estados").change(function(){
     var id = $(this).val();
     // var id = <?php echo $visualizar->Cidades; ?>;
        $.ajax({
            type:"POST",
            url:"exibe-cidades.php?id="+id,                    
            dataType:"text",
        success: function(res){                      
            $("#cidades").children(".cidades").remove();
            $("#cidades").append(res);
         }
      });
    });
  }); 

city displays.php

header( 'Cache-Control: no-cache' );
    header( 'Content-type: application/xml; charset="utf-8"', true );
      include("conexao.php");
    $id = $_GET['id'];
        $sql = mysqli_query($conexao, "SELECT * FROM crm_cidades WHERE estados_cod_estados='$id' ORDER BY nome");
        while($row=mysqli_fetch_array($sql)){
              $nome=$row['nome'];
              $id=$row['cod_cidades'];
              echo '<option value="'.$id.'" class="cidades">'.utf8_encode($nome).'</option>';
        }
  • Voce would have to somehow check whether this in an edition and already bring the select of the cities filled in according to the state and the city chosen with a selected

  • That. I’m already bringing the state, but I only need the cities related to the state that is registered in the database, which I’m bringing by the variable $visualize->Cities that is commented inside jquery.

  • On the editing page make a looping to bring the <option> filled

  • Hi, Daniel. I don’t think I was clear on my question. I can actually see the cities when I select the states, but in the register, in the field of cities, appears "Select the state above" and when selecting, lists the cities. What I want is that in the edition, already appear the selected state and city, but with the effect of selecting the cities and automatically appear the states, with the only difference and not appear the phrase and yes all cities.

  • Yeah, I get it, I guess I’m not making myself clear, to edit the form already comes with all the data filled in, right? So, you need to build an array of all cities based on the chosen state and generate the select theirs.

No answers

Browser other questions tagged

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