• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

html表单select下拉选项通过php从mysql动态获取的两种方法

武飞扬头像
没烧
帮助1

实现功能:mysql表新增行后,会成为html页面下拉菜单的新选项

 mysql数据库

学新通

方法一思路:

from表单中的select控件用div替代

php连接mysql,输出结果到div

方法一完整代码

test.html

  1.  
    <!doctype html>
  2.  
    <html>
  3.  
     
  4.  
    <head>
  5.  
    <meta charset="utf-8" />
  6.  
    <title></title>
  7.  
    </head>
  8.  
     
  9.  
    <body>
  10.  
    <!--这里只是用button触发事件,可以写在其它事件里,比如onload-->
  11.  
    <button onclick="ssincu()">点击显示下拉菜单</button>
  12.  
     
  13.  
    <form>
  14.  
    <div id="ccc"></div>
  15.  
    </form>
  16.  
    </body>
  17.  
    <script>
  18.  
    function ssincu(){
  19.  
    if (window.XMLHttpRequest) {
  20.  
    // IE7 , Firefox, Chrome, Opera, Safari 浏览器执行代码
  21.  
    xmlhttp = new XMLHttpRequest();
  22.  
    }
  23.  
    else {
  24.  
    // IE6, IE5 浏览器执行代码
  25.  
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  26.  
    }
  27.  
    xmlhttp.onreadystatechange = function () {
  28.  
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  29.  
    document.getElementById("ccc").innerHTML = xmlhttp.responseText;
  30.  
    }
  31.  
    }
  32.  
    xmlhttp.open("GET", "test.php" ,true);
  33.  
    xmlhttp.send();
  34.  
    }
  35.  
    </script>
  36.  
    </html>
学新通

test.php

  1.  
    <?php
  2.  
     
  3.  
    //连接数据库
  4.  
    $con = mysqli_connect('localhost','root','123');
  5.  
    if (!$con)
  6.  
    {
  7.  
    die('Could not connect: ' . mysqli_error($con));
  8.  
    }
  9.  
    // 选择数据库
  10.  
    mysqli_select_db($con,"opss");
  11.  
    // 设置编码,防止中文乱码
  12.  
    mysqli_set_charset($con, "utf8");
  13.  
     
  14.  
    //查询语句
  15.  
    $sql="SELECT cs FROM tb_test";
  16.  
     
  17.  
    $result = mysqli_query($con,$sql);
  18.  
     
  19.  
     
  20.  
    //把数据集合转化为二维数组
  21.  
    $arr = [];
  22.  
    while ($row = mysqli_fetch_assoc($result)){
  23.  
    $arr[] = $row;
  24.  
    }
  25.  
     
  26.  
    //输出下拉菜单
  27.  
    echo '<select name="incu">';
  28.  
     
  29.  
    //循环输出下拉菜单选项
  30.  
    foreach($arr as $incuarr){
  31.  
    echo '<option>';
  32.  
    echo '' . $incuarr['cs'] . '';
  33.  
    echo '</option>';
  34.  
    }
  35.  
    echo '</select>';
  36.  
     
  37.  
    mysqli_free_result($result);
  38.  
    mysqli_close($con);
  39.  
    ?>
  40.  
     
  41.  
     
  42.  
     
学新通


方法二完整代码:

test.html

  1.  
    <!doctype html>
  2.  
    <html>
  3.  
     
  4.  
    <head>
  5.  
    <meta charset="utf-8" />
  6.  
    <title></title>
  7.  
    </head>
  8.  
     
  9.  
    <body>
  10.  
    <form>
  11.  
    <select id="incu"></select>
  12.  
    </form>
  13.  
     
  14.  
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
  15.  
    <script type="text/javascript">
  16.  
    $(document).ready(function () {
  17.  
    var optHtml = $.ajax({ url: "/test.php", async: false });
  18.  
    $("#incu").html(optHtml.responseText);
  19.  
    });
  20.  
    </script>
  21.  
    </body>
  22.  
     
  23.  
    </html>
学新通

test.php

  1.  
    <?php
  2.  
    //连接数据库
  3.  
    $con = mysqli_connect('localhost','root','123');
  4.  
    if (!$con)
  5.  
    {
  6.  
    die('Could not connect: ' . mysqli_error($con));
  7.  
    }
  8.  
    // 选择数据库
  9.  
    mysqli_select_db($con,"opss");
  10.  
    // 设置编码,防止中文乱码
  11.  
    mysqli_set_charset($con, "utf8");
  12.  
    //查询语句
  13.  
    $sql="SELECT * FROM tb_test";
  14.  
     
  15.  
    $result = mysqli_query($con,$sql);
  16.  
    //把数据集合转化为二维数组
  17.  
    $arr = [];
  18.  
    while ($row = mysqli_fetch_assoc($result)){
  19.  
    $arr[] = $row;
  20.  
    }
  21.  
    //输出选项
  22.  
    foreach ($arr as $val){
  23.  
    echo "<option value='" .$val['id']. "'>".$val['cs']."</option>";
  24.  
    }
  25.  
    mysqli_close($con);
  26.  
    ?>
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgffbka
系列文章
更多 icon
同类精品
更多 icon
继续加载