dtp87205 2016-04-28 06:00
浏览 40

显示下拉选择的数据

I want to display data from database when I select an option from a dropdown option in php

<html>
<head>
    <script>
        function showUser(str) {
            if (str == "") {
                document.getElementById("txtHint").innerHTML = "";
                return;
            } else { 
                if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("txtHint").innerHTML =                     xmlhttp.responseText;
    }
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
    <?php
    include("db_connection.php");
    $name="select name from supplier_name";
    ?>
    <form method="POST" action="getuser.php">
        <tr>
            <td>Name</td>
            <td><?php echo "<select name='name'    
                onchange='showUser(this.name)'><option value=''>select 
                name</option>";
                foreach ($con->query(@$name)as $ridyn)
                {
                    echo "<option value='$ridyn[name]'>$ridyn[name]</option>";
                }
                echo"</select>"; ?></td>
            </tr>
        </form>
        <br>
        <div id="txtHint"><b>You have selected......</b></div>
    </body>
    </html>
  • 写回答

1条回答 默认 最新

  • douchun2158 2016-04-28 07:30
    关注

    Here is a jQuery example (I am not really familiar with straight javascript ajax). You should be able to fill it in from this example.

    // Have a function that returns your names
    function getSuppliers($con)
        {
            $query  =   $con->query("select `name` from `supplier_name`");
            while($result = $query->fetch(PDO::FETCH_ASSOC)) {
                $row[]  =   $result;
            }
    
            return (!empty($row))? $row : array();
        }
    
    // Include database con at top
    include("db_connection.php");
    
    ?>
    <html>
    <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <script>
    $(document).ready(function(){
        // On menu-change
        $(this).on('change','.ajax_change',function(e){
            $.ajax({
                // Get the url from the action on the form
                url: $('#getUser').attr('action'),
                // Get the value of the dropdown
                data: { value: $(this).val() },
                type: 'post',
                success: function(response) {
                    // Print to the text-hint div
                    $('#txtHint').html(response);
                }
            });
        });
    });
    </script>
    </head>
    <body>
    <form method="POST" action="getuser.php" id="getUser">
        <table>
            <tr>
                <td>Name</td>
                <td>
                    <select name='name' class="ajax_change">
                        <option value=''>Select name</option>
    <?php   foreach(getSuppliers($con) as $ridyn) {
    ?>                  <option value="<?php echo $ridyn['name']; ?>"><?php echo $ridyn['name']; ?></option>
    <?php   }
    ?>              </select>
                </td>
            </tr>
        </table>
    </form>
    <br />
    <div id="txtHint"><b>You have selected......</b></div>
    </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比