简体   繁体   English

如何根据其他组合框的选定值填充组合框?

[英]How can i populate a combobox depending on the selected value of a different combobox?

All I want to do is send the value of the option selected from slct1 in my cb_insumo_update.php and also populate slct1 with cb_insumo_update.php without needing to refresh the page or using a form button. 我要做的就是在我的cb_insumo_update.php发送从slct1中选择的选项的值,并用cb_insumo_update.php填充slct1 ,而无需刷新页面或使用表单按钮。

I tried ajax but I'm kind of new to it so I don´t know how to make it work. 我尝试过ajax,但是对它有些陌生,所以我不知道如何使它起作用。

The thing is that I'm using a database to populate the select tags and kinda got confused along the way: 事实是,我正在使用数据库填充选择标签,并且在使用过程中有些困惑:

<section class="container">

    <div class="row" >

      <div class="input-field  col s12" >
        <select id="slct1" name="slct1" onchange="populate('slct1','slct2')">
          <?php  require('php/cb_categoria_update.php');  ?>
        </select>
        <label>Categoría</label>
      </div>


      <div class="input-field col s12" method="GET">
        <select id="slct2" name="slct2">
         </select>  
        <label>Insumo</label>
      </div>

    </div>
</section>

cb_categoria_update cb_categoria_update

<?php 

require('Connect.php');

$CB_cate = mysqli_query($enlace,"SELECT * FROM vw_display_catego_insumo");

    echo "<option empty disabled selected>Elija una opción</option>";

while($row = mysqli_fetch_array($CB_cate))
{
    $idcat = $row['fn_idCategoInsumo'];
    $nomcat = $row['fc_NomCategoInsumo'];


    echo "<option value='" . $idcat . "'>" . $nomcat . "</option>";

}


mysqli_close($enlace);

?> 

cb_insumo_update cb_insumo_update

<?php 

require('Connect.php');


 $cateinsumo=$_POST['cbidcategoria'];


    $spinsumo="call SP_INSUMOS_BY_CAT('".$_POST['cbidcategoria']."')";

    $sqlspinsumo = mysqli_query($enlace, $spinsumo); 

    $sqlarray = mysqli_fetch_array($sqlspinsumo);


    echo "<option disabled selected>Elija una opción</option>";



while($row = mysqli_fetch_array($sqlspinsumo))
{
    $idinsumo = $row['fn_IdInsumo'];
    $nominsumo= $row['fc_NomInsumo'];

    echo "<option value='" . $idinsumo . "'>" . $nominsumo . "</option>";

}


mysqli_close($enlace);

?>

I will use jQuery as an example, it's just easier to throw together than vanilla JavaScript, so you would need the jQuery library in your header (or at the bottom of your page): 我将以jQuery为例,它比普通的JavaScript更容易组合在一起,因此您需要在标头(或页面底部)中使用jQuery库:

<section class="container">
    <div class="row" >
      <div class="input-field col s12" >
        <select id="slct1" name="slct1">
          <!-- You can keep this as is -->
          <?php require('php/cb_categoria_update.php');  ?>
        </select>
        <label>Categoría</label>
      </div>
      <div class="input-field col s12" method="GET">
        <!-- Use a jQuery event listener instead of the inline onchange -->
        <select id="slct2" name="slct2">
         </select>  
        <label>Insumo</label>
      </div>
    </div>
</section>

<script>
// Document ready
$(function(){
    // Listen for change on first drop down
    $('#slct1').on('change', function(){
        // Run ajax
        $.ajax({
            // Set the url for the file you want to load into current page
            'url': 'cb_insumo_update.php',
            // You could use GET here, I just use post
            'type': 'post',
            // Fetch the value of the current selected option
            'data': $(this).val(),
            // Take the html result from the php page
            'success': function(response) {
                // Place it into this page
                $('#slct2').html(response);
            }
        });
    });
});
</script>

An important note, you will want to bind parameters on this query: 重要说明,您将希望在此查询上绑定参数:

$spinsumo="call SP_INSUMOS_BY_CAT('".$_POST['cbidcategoria']."')";

This is not a secure way to run SQL queries. 这不是运行SQL查询的安全方法。

Alternately, you can do the "Shorthand" methods for the ajax found on this page . 或者,您可以对在此页面上找到的ajax执行“快捷”方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM