Problems with dynamic select - dependent ajax, mysql and php

1

I try to create a dynamic / dependent select with ajaxx and this even though it generates return results, I can not give value to the html element. Here the code:

 <script>
      // Agarra el ajaxx de país.
    $(document).ready(function(){
              $('#paisselect').on('change',function(){ 
              var pais=$('select#paisselect').find(":selected").val();
               if(pais){
                console.log(pais);
                    $.ajax({
                        data:{pais:pais},
                        type:'POST', //mandar variables como post o get
                        url:'config/database.php', //url que recibe las variables
                        dataType :'html',
                        success:function(html){
                          $("#region").html(html);
                        }
                   });
               }            
  });     
});     
  </script>

html:

<label for="region"> Estado/Region: </label>
<select class="form-control" id="exampleFormControlSelect2" value=""> 

      

PHP data query:

 if(!empty($pais) && !empty($pais)){

        $sql = "SELECT * FROM regiones where id_pais='.$pais.'";
        $resultado_regiones = $conn->query($sql);
        if($resultado_regiones->num_rows > 0) 
        {
        while($rowsregiones = $resultado_regiones->fetch_object())
        {       
           echo '<option>'.$rowsregiones->nombre.' </option';
        }
        }


    } 

Thankful I would be with the help.

    
asked by Angel Gomez 15.12.2018 в 03:33
source

2 answers

1

In the success of the ajax call you want to insert the results in the element with id "region" but the select does not have the appropriate id attribute. You must put the attribute id="region"

<select class="form-control" id="region" value="">

Although I do not think this will interfere with you, you have forgotten the symbol ">" in your closing tag of the options:

echo '<option>'.$rowsregiones->nombre.' </option';
    
answered by 15.12.2018 в 03:51
1

There are some errors:

  • In the success you assign the results to an element whose id would be region , but the select that you have has another id.
  • In PHP your option are not created with the proper format that would be: <option value="value1">Value 1</option> and there are syntax errors. You also make two identical evaluations in if

We will correct:

HTML

<label for="region"> Estado/Region: </label>
<select class="form-control" id="region">
</select> 

Make sure that no other item has the id region .

PHP

if( !empty($pais) )
{

    $sql = "SELECT * FROM regiones where id_pais='.$pais.'";
    $resultado_regiones = $conn->query($sql);
    if($resultado_regiones->num_rows > 0) 
    {
        while($rowsregiones = $resultado_regiones->fetch_object())
        {       
            $region=$rowsregiones->nombre; 
            echo "<option value=\"$region\">$region</option>";
        }
    }
} 

In value , if you want, you can put another data, such as the id of the region if it exists.

    
answered by 15.12.2018 в 03:51