Combo Box does not return the Select Data with AJAX

0

I have made a combo box that should return the bank account numbers of a bank, because a bank can have more than one account number registered, I am sending a query through AJAX to the database I have inserted an HTML in the option which shows the account number according to the selection of the User's bank, I attach the html code, the AJAX and the query. Please I appreciate your help.

<script type="text/javascript"> $(document).ready (function () {
  $("#cbx_banco").change(function(){
    $("#cbx_banco option:selected").each(function(){
      var idbanco = fk_id_banco = $(this).val();

      alert (idbanco);

      var banco = fk_id_banco;

      $.ajax({
        type:"POST",
        url:"getbankacounts.php",
        data: banco,
        success: function(data)
          $("#cbx_counts").html(data);
        })
      });
    });
  });
</script>

This is the AJAX destination code where I make the query

<?php


require('conexion.php');


$fk_id_banco = $_POST['fk_id_banco'];

$queryCb = "select nr_Cuenta_bancaria from cuenta_bancaria where fk_id_Banco 
= '$fk_id_banco' ";

$resultadoCb = $mysqli->query($queryCb);

$html = "<option value='0'>Seleccionar Cuenta </option>";


while ($opcionesCta = $resultadoCb->fecth_assoc()){


$html = "<option value = 


'".$opcionesCta['nr_Cuenta_bancaria']."'>".
$opcionesCta['nr_Cuenta_bancaria']."</option>";

}

echo $html;

And this is the HTML fragment where you should show the account number according to what the user selects as a bank.

<select id="cbx_counts" class="custom-select" name="cbx_counts" >

<option value="">Nro Cuenta: </option>
<option value=""><?php $opciones['nr_Cuenta_bancaria'] ?></option>

</select>
    
asked by gerardo Herrera 15.12.2018 в 15:21
source

1 answer

0

Dear I Had Several Errors here I put the code that works for me Perfect. I appreciate the suggestions you can give me about it now I must send a query with the selected data.

Script:

 <script type="text/javascript"> $(document).ready (function () {
                    $("#cbx_banco").change(function(){
                        $("#cbx_banco option:selected").each(function(){
                            var idbanco = fk_id_banco = $(this).val();

                                  var banco = fk_id_banco;

                              $.ajax({
                                type: 'post',
                                url: 'obtenercuenta.php',
                                data: {idbanco:idbanco},
                                success: function(datos)
                                {
                                  $("#cbx_counts").html(datos);
                                }
                            })
                        });
                    });
                 });


    </script>

Here PHP of the Consultation

<?php


$conexion = mysqli_connect('localhost','gerardo','1234','bghdhdhdhnjjj');


 $fk_id_banco = $_POST["idbanco"];


$sql = "select nr_Cuenta_bancaria from cuenta_bancaria where fk_id_Banco 
 ='$fk_id_banco'";


$consulta = mysqli_query($conexion,$sql);



 $html = "<option value='0'>Seleccionar Cuenta </option>";



 while ($opciones = mysqli_fetch_array($consulta)):; ?>

  <option id="fk_id_banco" selected="selected" <?php echo 
     $opciones['nr_Cuenta_bancaria']; ?>"><?php echo $opciones['nr_Cuenta_bancaria']; 
     ?></option>


    <?php endwhile; ?>

Here is the HTML where it is displayed

   <select id="cbx_counts" class="custom-select"  name="cbx_counts" >

   <option value="">Nro Cuenta: </option>

   <option value=""><?php $opciones['nr_Cuenta_bancaria'] ?></option>

   </select>
    
answered by 28.12.2018 / 03:42
source