【问题标题】:how to set textbox value from sql database on changing combo box option?如何在更改组合框选项时从 sql 数据库中设置文本框值?
【发布时间】:2013-06-21 06:53:11
【问题描述】:

我有一个让我很困惑的学校项目(虽然看起来很简单).. 所以这是交易,我需要根据组合框的选择设置文本框的值。这是我到目前为止得到的结果

    <?php require_once( str_replace('//','/',dirname(__FILE__).'/') . 'library/SfCrud.php' );   
      $sfCrud = new SfCrud();
      $resultSupplier = $sfCrud->read('select * from supplier');
    ?>

     <form action="" method="post">
     <div align="center">
       <table>  
       <tr>
         <td colspan="3">&nbsp;</td>
       </tr>
       <tr>
         <td colspan="3">Supplier Information</td>
       </tr>
       <tr>
         <td>Supplier  </td>
         <td>:</td>
         <td>
           <select style="width:100%" id='slcSupplier' name="slcSupplier">
             <?php foreach($resultSupplier as $row){?>
         <option  value="<?php echo($row->supplierID);?>"> <?php echo($row->supplierID.' : '.$row->name);?>
             </option> <?php }?>
           </select>
         </td>
       </tr>
       <tr>
         <td>Address</td><td>:</td>
         <td> <textarea id="txtAddress" name="txtAddress"></textarea> </td>
       </tr>
       </table>
     </form>

我需要从数据库中获取所选供应商的地址,并在不刷新整个页面的情况下将其显示在文本区域中..感谢您的帮助..

我在阅读答案后修改了我的代码

   <?php require_once( str_replace('//','/',dirname(__FILE__).'/') . 'library/SfCrud.php' );    
   $sfCrud = new SfCrud();
   $resultSupplier = $sfCrud->read('select * from supplier');

   ?>

   <script>
   $(document).ready(function(e)
   {
  $("#slcSupplier").change(function(){
    var id = $(this).val();

    $.ajax({
        url: 'supplier.php',
        method: 'POST',
        data: "id=" + id,
        success: function(data)
        {
            $('#txtAddress').html(data);
        }
    }); 
});
    });
    </script>

    <div> 

    <table> 
     <tr>
       <td colspan="3">
         <input type="button" name="a" id="a" value="Submit" /></td>
     </tr>
     <tr>
       <td colspan="3">Supplier Information</td>
     </tr>
     <tr>
      <td>Supplier  </td>
      <td>:</td>
      <td>
        <select style="width:100%" id='slcSupplier' name="slcSupplier">
        <?php foreach($resultSupplier as $row){?>
        <option  value="<?php echo($row->supplierID);?>">
              <?php echo($row->supplierID.' : '.$row->name);?>
            </option> <?php 
     }?>
        </select>
      </td>
     </tr>
     <tr>
       <td>Address</td><td>:</td>
       <td>
          <textarea id="txtAddress" name="txtAddress"></textarea>
       </td>
     </tr>


  </table>

我不知道出了什么问题,但是 javascript 不起作用.. 我在这里很无能为力..

【问题讨论】:

    标签: php jquery sql ajax


    【解决方案1】:

    在标题中包含 jquery

    创建另一个名为newpage.php 的文件,其中包含以下概念

    $supplier_id = $_POST['supplier_id']; //read the supplier id from the post
    
     $query = "SELECT blah,blah FROM supplier WHERE id = " . $supplier_id; //build a query to retrieve the required field
    
     $row = fetch_row($query); //execute the query and get the results.
    
     $json = array('field' => $row['field1'], 'field2' => $row['field2']);
    
     echo json_encode($json);
    

    然后在您当前的 html 页面中输入此代码

    $(document).ready(function()
    {
        $('#clcSupplier').change(function()
        {
            var id = $(this).val();
    
            $.ajax({
                url: 'newpage.php',
                type: 'POST',
                dataType: 'json',
                data: "supplier_id=" + id,
                success: function(data)
                {
                    $('#txtAddress').val(data.field + data.field2);
                }
            })
        });
    });
    

    【讨论】:

      【解决方案2】:

      AJAX

      $(document).ready(function()
      {
          $('#slcSupplier').change(function()
          {
              var id = $(this).val();
      
              $.ajax({
                  url: 'your_php.php',
                  type: 'POST',
                  data: { id: id },
                  success: function(data)
                  {
                      $('#txtAddress').text(data);
                  }
              })
          });
      });
      

      PHP

      if(isset($_POST['id']))
      {
          $id = $_POST['id'];
      
          $query = $this->db->query("SELECT address FROM supplier WHERE id =". $id);
          if($query)
          {
              while($row = $query->fetch(PDO::FETCH_ASSOC))
              {
                  echo $row['address'];
              }
          }
      }
      

      【讨论】:

      • javascript 不起作用.. 我写的代码可能有问题,但我无法弄清楚。看我的转贴。
      • @KENI 将 method 替换为 type ,在 method: 'POST' 中将其更改为 type: 'POST'
      猜你喜欢
      • 2018-01-29
      • 2013-02-01
      • 1970-01-01
      • 2016-12-06
      • 2021-08-17
      • 2019-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多