dshmvqnl98119 2018-09-19 01:30
浏览 59

从数据库中检索选定的下拉值以回显该值

retrieve the selected dropdown value from data base echo the value and it didnot triggered the 2nd dropdown value using ajax. I have a form to create an activities. The dropdown ajax for program and category is working well when creating new activities. But once i try to display the activities that i have created which is edit form and i retrieve all the value, the ajax program and category is not working

Here my two dropdown program and catergory this two is dependent.

Here is button

<li><a style="text-align:left" class="btn waves-effect waves-light" data-toggle="modal" data-target="#<?php echo $rowss['id']; ?>">Edits</a></li>

Here is modal

<div class="modal fade" id="<?php echo $rows["id"]; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="form-group">
                <label for="programs" class="col-sm-3 control-label">Program</label>
                <div class="col-sm-9">
                <select name="programs" class="programs form-control" id="programs" onchange="get_category(this);">

                <?php 
                foreach($programs as $row)
                {
                if($row['tcp_id']==$eveprrm['tcp_id'])
                {
                ?>
                    <option value="<?php echo $row['tcp_id']; ?>" selected><?php echo $row['tcp_name']; ?></option>
                    <?php 
                    } else {
                    ?>
                    <option value="<?php echo $row['tcp_id']; ?>"><?php echo $row['tcp_name']; ?></option>
                    <?php 
                        }
                    }
                    ?>
                </select>
                </div>
            </div>

            <div class="form-group">
                <label for="categorys" class="col-sm-3 control-label">Category</label>
                <div class="col-sm-9">
                    <select name="categorys" class="form-control" id="categorys">
                    </select>
                </div>
            </div>
        </div>
    </div>
</div>

when i fetch and echo the option value for program the category dropdown didnot triggered the javascript for the ajax. Below is my script for ajax.

<script>
    function get_category(sel)
{
    var tcp_id = sel.options[sel.selectedIndex].value; 
    if(tcp_id){
    $.ajax({
    type:'POST',
    url:'fetch_data.php',
    data:'tcp_id='+tcp_id,
    cache: false,

    success:function(html)
    {
        $('#categorys').html(html);

    }
    }); 
    }
    else
    {
        $('#categorys').html('<option value="">Select category first</option>');\
    }
}
</script>

and this is my fetch_data.php code

if(isset($_POST["tcp_id"]) && !empty($_POST["tcp_id"]))
{
    $progs = $bdd->prepare("SELECT * FROM category_new WHERE tca_program_id = ".$_POST['tcp_id']);
    $progs->execute();
    $rows = $progs->fetchAll();

    echo '<option value="">Select category first</option>';
    foreach($rows as $row)
    { 
        echo '<option value="'.$row['tca_id'].'">'.$row['tca_name'].'</option>';
    }

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度