dongxibo2095 2015-09-23 07:36
浏览 24

php,mysql和javascript

I want to do dynamic select option like when selected dropdown option of first column in database immediately I want to display data of second column from mysql database in input field.

<select name="name" class="form-control" autoFocus="" id="clients" onchange="myFunction()">
    <option value="">Select Client</option>
    <?php 
        $query = mysql_query("SELECT * FROM clients");
        while($selectclients = mysql_fetch_array($query)){  
   ?>
    <option id="<?php echo $selectclients['client_id']; ?>" value="<?php echo $selectclients['data1'] ?>" onchange="show(this.value);"></option>
   <?php } ?>   
</select>

<span name="data2" type="text" id="data2"></span>
<script>
function myFunction() {
    var x = document.getElementById("clients").value;
    document.getElementById("list_currency").innerHTML = "<input name='currency' type=text id='currency' size='12' value="+ x +">";
}
</script>

In the script i have used m able to fetch the data and display in dropdown option and when select (first column in database) whatever m selecting in select option m able to display in input field but i m not able to display data from second column of database

I kindly request to help me in this issue

  • 写回答

2条回答 默认 最新

  • dqf35839 2015-09-23 08:29
    关注
    <?php 
        $query = mysql_query( 'select * from `clients`;' );
    
        echo "
        <select name='name' class='form-control' autoFocus='' id='clients' onchange='myFunction()'>
            <optgroup label='Select Client'>";
            while( $rs = mysql_fetch_array( $query ) ){  
                echo "<option id='{$rs['client_id']}' value='{$rs['data1']}'>{$rs['data1']}</option>";
            }
        echo "  
            </optgroup>
        </select>";
    ?>
    
    
    /* span elements do not have a 'type' attribute */
    <span name='data2' id='data2'></span>
    /* Your original code did not show the receiving html element, list_currency */
    <span name='list_currency' id='list_currency'></span>
    
    
    <script type='text/javascript'>
        function myFunction() {
            var oSelect = document.getElementById('clients');
            var oCur=document.getElementById('list_currency');
            var value=oSelect.options[ oSelect.options.selectedIndex ].value;
    
            oCur.innerHTML = "<input name='currency' type='text' id='currency' size='12' value=" + value + ">";
        }
    </script>
    

    I'm not 100% sure I understood the question as there is mention of data from a second column but no reference to it in the code. That said, based on the original when you select an option from the dropdown menu the container element list_currency will be populated with the input field with the value of the select menu. If this is NOT what you meant then please explain further - especially what the second column is...

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题