Select dependent save id, but I need to save the variable

0
<code><!-- desde aca empieza select dependiente -->
       <?php
        include('db-config.php');
     $query = $db->query("SELECT * FROM provincias ORDER BY provincia ASC");
        $rowCount = $query->num_rows;
       ?>
       <div class="input-group input-group-lg">
        <span class="input-group-addon" id="sizing-addon1">
          <i class="glyphicon glyphicon-map-marker"></i>
        </span>
       <select name="us_provincia" id="provincia" 
           value="<?php echo $usprovincia; ?>">
       <option value="">Seleccione Provincia</option>

       <?php
         if($rowCount > 0){
           while($row = $query->fetch_assoc()){
   echo '<optionvalue="'.$row['id_prov'].'">'.$row['provincia'].'</option>';
           }
         }else{
        echo '<option value=""> no disponible</option>';
         }
        ?>
         </select>
         </div>

         <div class="input-group input-group-lg">
          <span class="input-group-addon" id="sizing-addon1">
           <i class="glyphicon glyphicon-map-marker"></i></span>
       <select name="us_ciudad" id="ciudad" value="<?php echo $usciudad; >">
          <option value="">ciudad</option>
       </select>
        </div>

        <script src="../js/jquery.min.js"></script>
        <script type="text/javascript">
          $(document).ready(function(){
        $('#provincia').on('change',function(){
         var provinciaID =$(this).val();
          if(provinciaID){
         $.ajax({
         type:'POST',
         url:'ajaxData.php',
         data:'id_prov='+provinciaID,

         success:function(html){
         $('#ciudad').html(html);
          }
           });
            }else{
          $('#ciudad').html('<option value="">Ciudad</option>');
             }
           });
          });
        </script>

        <!-- aca termina select dependiente -->
</code>

I understand that I keep the id only because it is what I am doing, but I would like an idea of how to save the names of the variables, not their id.

And the ajaxData.php page:

<code>
    <?php
     include('db-config.php');
      if(isset($_POST["id_prov"]) && !empty($_POST["id_prov"])){
       $query = $db->query("SELECT * FROM ciudades
         WHERE id_prov =".$_POST['id_prov']." ORDER BY ciudad ASC");
           $rowCount = $query->num_rows;
       if($rowCount > 0){
         echo '<option value="">Seleccione ciudad</option>';
       while($row = $query->fetch_assoc()){
         echo '<option
      value="'.$row['id_ciudad'].'">'.$row['ciudad'].'</option>';
       }
        }else{
           echo '<option value="">no disponible</option>';
        }
      }
    ?>
</code>
    
asked by Alberto Charly 24.07.2018 в 05:24
source

1 answer

0

You can access the text of the variable selected from the onchange as shown in the example. Once stored, you can send it as part of the data of ajax as well as the id.

$('#provincia').on('change',function(){
 var provinciaID =$(this).val();
 var provinciaText = $(this).find('option:selected').text();
//.....
 alert(provinciaID + "---" + provinciaText);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<select id="provincia">
 <option value="1">Texto1</option>
 <option value="2">Texto2</option>
 <option value="3">Texto3</option>
</select>
    
answered by 24.07.2018 в 09:43