• <i id='hl2PQ'><tr id='hl2PQ'><dt id='hl2PQ'><q id='hl2PQ'><span id='hl2PQ'><b id='hl2PQ'><form id='hl2PQ'><ins id='hl2PQ'></ins><ul id='hl2PQ'></ul><sub id='hl2PQ'></sub></form><legend id='hl2PQ'></legend><bdo id='hl2PQ'><pre id='hl2PQ'><center id='hl2PQ'></center></pre></bdo></b><th id='hl2PQ'></th></span></q></dt></tr></i><div id='hl2PQ'><tfoot id='hl2PQ'></tfoot><dl id='hl2PQ'><fieldset id='hl2PQ'></fieldset></dl></div>
        <bdo id='hl2PQ'></bdo><ul id='hl2PQ'></ul>
    1. <small id='hl2PQ'></small><noframes id='hl2PQ'>

      <tfoot id='hl2PQ'></tfoot>
    2. <legend id='hl2PQ'><style id='hl2PQ'><dir id='hl2PQ'><q id='hl2PQ'></q></dir></style></legend>

      1. 使用 PHP &amp;MySQL 填充下拉列表

        时间:2023-10-02
        <i id='n3PHv'><tr id='n3PHv'><dt id='n3PHv'><q id='n3PHv'><span id='n3PHv'><b id='n3PHv'><form id='n3PHv'><ins id='n3PHv'></ins><ul id='n3PHv'></ul><sub id='n3PHv'></sub></form><legend id='n3PHv'></legend><bdo id='n3PHv'><pre id='n3PHv'><center id='n3PHv'></center></pre></bdo></b><th id='n3PHv'></th></span></q></dt></tr></i><div id='n3PHv'><tfoot id='n3PHv'></tfoot><dl id='n3PHv'><fieldset id='n3PHv'></fieldset></dl></div>
        • <bdo id='n3PHv'></bdo><ul id='n3PHv'></ul>
            <tfoot id='n3PHv'></tfoot>
              <legend id='n3PHv'><style id='n3PHv'><dir id='n3PHv'><q id='n3PHv'></q></dir></style></legend>
                <tbody id='n3PHv'></tbody>

                  <small id='n3PHv'></small><noframes id='n3PHv'>

                1. 本文介绍了使用 PHP &amp;MySQL 填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试使用从使用 PHPMySQL 的第一个下拉列表中选择的值填充第二个下拉列表,并且不刷新页面.我认为这很简单,但无法使其正常工作,因此非常感谢您的帮助.

                  I am trying to populate a second dropdown list using the value selected from a first dropdown list using PHP and MySQL, and without refreshing the page. I thought this would be simple but can't get it to work so any help would be much appreciated.

                  到目前为止,我有以下几点:

                  So far, I have the following:

                  HTML 表单 (form.php)

                  <select name="list1" id="list1">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                  </select>
                  
                  <select name="list2" id="list2">
                  
                  </select>
                  

                  JavaScript(在 form.php 中)

                  <script type="text/javascript">
                    $("#list1").change(function() {
                      $("#list2").load("get_list2.php?id=" + $("#list1").val());
                    });
                  </script>
                  

                  get_list2.php

                  require_once("config.php");
                  
                  $q1 = mysql_query("SELECT * FROM mytable WHERE id = '$_GET[id]'");
                  while($row1 = mysql_fetch_assoc($q1)){
                    echo "<option>".$row1['item']."</option>";
                  }
                  

                  谢谢!

                  推荐答案

                  就像其他成员所说的,你应该使用 PDO(带有准备好的语句)而不是 mysql_.

                  Like other members have says, you should use PDO (with prepared statements) instead of mysql_.

                  一种可能的实现:

                  HTML (form.php)

                  HTML (form.php)

                  <select name="list1" id="list1">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                  </select>
                  
                  <select name="list2" id="list2"></select>
                  
                  <script type="text/javascript">
                  $("#list1").change(function() {
                      $.ajax({
                          url : "get_list2.php?id=" + $(this).val(),                          
                          type: 'GET',                   
                          dataType:'json',                   
                          success : function(data) {  
                              if (data.success) {
                                  $('#list2').html(data.options);
                              }
                              else {
                                  // Handle error
                              }
                          }
                      });
                  });
                  </script>
                  

                  PHP (get_list2.php)

                  PHP (get_list2.php)

                  require_once("config.php");
                  
                  $id = $_GET['id'];
                  
                  if (!isset($id) || !is_numeric($id))
                      $reponse = array('success' => FALSE);
                  else {
                      // Where $db is a instance of PDO
                  
                      $query = $db->prepare("SELECT * FROM mytable WHERE id = :id");
                      $query->execute(array(':id' => $id));
                      $rows = $query->fetchAll(PDO::FETCH_ASSOC);
                  
                      $options = "";
                      foreach ($rows as $row) {
                          $options .= '<option value="'. $row .'">'. $row .'</option>';
                      }
                  
                      $response = array(
                          'success' => TRUE,
                          'options' => $options
                      );
                  }
                  
                  header('Content-Type: application/json');
                  echo json_encode($response);
                  

                  PS:没有经过测试,但它应该可以工作......我猜.

                  PS : not tested but it should works... I guess.

                  这篇关于使用 PHP &amp;MySQL 填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:更改第一个 DropDownList 后如何从数据库加载第二个 下一篇:复选框仅适用于 jQuery 数据表中的当前分页页面

                  相关文章

                  最新文章

                2. <i id='47QUY'><tr id='47QUY'><dt id='47QUY'><q id='47QUY'><span id='47QUY'><b id='47QUY'><form id='47QUY'><ins id='47QUY'></ins><ul id='47QUY'></ul><sub id='47QUY'></sub></form><legend id='47QUY'></legend><bdo id='47QUY'><pre id='47QUY'><center id='47QUY'></center></pre></bdo></b><th id='47QUY'></th></span></q></dt></tr></i><div id='47QUY'><tfoot id='47QUY'></tfoot><dl id='47QUY'><fieldset id='47QUY'></fieldset></dl></div>
                      <bdo id='47QUY'></bdo><ul id='47QUY'></ul>
                    <tfoot id='47QUY'></tfoot>

                    1. <small id='47QUY'></small><noframes id='47QUY'>

                      <legend id='47QUY'><style id='47QUY'><dir id='47QUY'><q id='47QUY'></q></dir></style></legend>