doufu1950 2016-10-08 19:52
浏览 64

根据使用相同SQL表的下拉列表自动填充文本字段

I'm trying to populate a text field and a text area depending on the choice of the second dropdown list using an SQL table. Somehow I got it all wrong. Here's my code right now:

AJAX/HTML

<script>
  function getPackage(val) {
   $.ajax({
   type: "POST",
   url: "get_package.php",
   data:'serviceid='+val,
   success: function(data){
      $("#package-list").html(data);
       }
    });
   }

   function getPackageDesc(val) {
   $.ajax({
   type: "POST",
   url: "get_packagedesc.php",
   data:'packageid='+val,
   success: function(data){
      $("#price").html(data);
      }
    });
   }

   function selectService(val) {
   $("#search-box").val(val);
   $("#suggesstion-box").hide();
     }
</script>

I feel like there is already something wrong with function getPackageDesc. I don't know if it's in this function or in the php file.

MAIN HTML

<div class="frmDronpDown">
 <div class="row">
   <label>Service:</label>
   <br/>
   <select name="service" id="service-list" class="demoInputBox" onChange="getPackage(this.value);">
     <option value="">Select Service</option>
     <?php
      foreach($results as $service) {
     ?>
     <option value="<?php echo $service[" serviceid "]; ?>">
      <?php echo $service["servicename"]; ?>
     </option>
    <?php
      }
    ?>
    </select>
  </div>
  <div class="row">
    <label>Package:</label>
    <br/>
    <select name="package" id="package-list" class="demoInputBox" onChange="getPackageDesc(this.value);">
      <option value="">Select Package</option>
    </select>
  </div>
  <div class="row">
    <label>Package Information:</label>
    <br/>
    <br/> Price:
    <br>
    <input type="text" name="price" id="price">
    <br>
    <br> Package Description:
    <br>
    <textarea name="packagedesc" form="usrform" id="packagedesc"></textarea>
  </div>
</div>

get_package.php

<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["serviceid"])) {
    $query ="SELECT * FROM tbl_packages WHERE serviceid = '".$_POST["serviceid"]."'";
    $results = $db_handle->runQuery($query);
?> < option value = "" > Select Package < /option>
<?php
    foreach($results as $package) {
?> < option value = "<?php echo $package["
packageid "]; ?>" > <?php echo $package["packagename"]; ?> < /option>
<?php
    }
}
?>

This part of the code works. I have populated the dropdowns using two SQL tables, tbl_services and tbl_packages. The next php file is where I'm having trouble.

get_packagedesc.php

<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["packageid"])) {
    $query ="SELECT * FROM tbl_packages WHERE packageid = '".$_POST["packageid"]."'";
    $result = mysqli_query($conn, $query);
?>
<?php
    while($row = mysqli_fetch_array($result)){
    ?>
    <?php echo $price['price'] ; ?></br>    
    <?php echo $packagedesc['packagedesc']; ?>  </br>
<?php
    }
}
?>

This code is where I'm having trouble at. I don't know if it is okay to get data from the same SQL table (tbl_packages). Or maybe I messed up with the $result.

What is wrong with my code and what could be the solution?

Thanks for any help.

  • 写回答

1条回答 默认 最新

  • dongyu9667 2016-10-08 21:16
    关注

    It looks like you're not grabbing the information from the row itself but from undeclared variables. Try this:

    <?php echo $row['price'] ; ?></br>    
    <?php echo $row['packagedesc']; ?>  </br>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号