无法从选项值获取html数据

ecbunoof  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(347)

我正在获取所选选项值的id,但表数据不会随选项更改而显示。我没有犯错误,也找不到什么是错误。
Jmeter 板.php

<select id="employee">
                <option value="" selected="selected"></option>
            <?php
            $host    = "localhost";
            $user    = "root";
            $pass    = "";
            $db_name = "test2";
            $lastId="";

            //create connection
            $con = mysqli_connect($host, $user, $pass, $db_name);
            $sql = "SELECT asset_type,department,cost FROM track_data";
            $resultset = mysqli_query($con, $sql) or die("database error:". mysqli_error($conn));
            while( $rows = mysqli_fetch_assoc($resultset) ) { 
            ?>
            <option value="<?php echo $rows["id"]; ?>"><?php echo $rows["asset_type"]; ?></option>
            <?php } ?>
        </select>
            <div id="display" style="color: black">
          <div class="row" id="heading" style="color: black"><h3><div class="col-sm-4"><strong>Employee Name</strong></div><div class="col-sm-4"><strong>Age</strong></div><div class="col-sm-4"><strong>Salary</strong></div></h3></div><br>      
          <div class="row" id="records" style="color: black"><div class="col-sm-4" id="emp_name"></div><div class="col-sm-4" id="emp_age"></div><div class="col-sm-4" id="emp_salary"></div></div>     

        </div>
    <script type="text/javascript">
    $(function () { 
        // $("#show_table").show();
       $(document).ready(function(){  
      // code to get all records from table via select box
      $("#employee").change(function() {    
        var id = $(this).find(":selected").val();
        var dataString = 'id='+ id;    
        $.ajax({
          url: 'getEmployrr.php',
          dataType: "json",
          data: dataString,  
          cache: false,
          success: function(employeeData) {
             if(employeeData) {          
              $("#emp_name").text(employeeData.asset_type);
              $("#emp_age").text(employeeData.department);
              $("#emp_salary").text(employeeData.cost);
              $("#records").show();    
            } else {
              $("#heading").hide();
              $("#records").hide();
            }     
          } 
        });
      }) 
    });
    });
      </script>

getemployrr.php文件

<?php
        $host    = "localhost";
            $user    = "root";
            $pass    = "";
            $db_name = "test2";
            $lastId="";

            //create connection
            $con = mysqli_connect($host, $user, $pass, $db_name);
        if($_REQUEST['id']) {
            $sql = "SELECT asset_type,department,cost FROM track_data WHERE id='".$_REQUEST['id']."'";
            $resultset = mysqli_query($con, $sql) or die("database error:". mysqli_error($con));

            $data = array();
            while( $rows = mysqli_fetch_assoc($resultset) ) {
                $data = $rows;
            }
            echo json_encode($data);
        } else {
            echo 0; 
        }
        ?>

我得到了头,但无法得到div内的值。我没有得到任何类型的错误,但值没有显示。我怎样才能解决这个问题。

svujldwt

svujldwt1#

在第一个sql查询中:

$sql = "SELECT asset_type,department,cost FROM track_data";

你没有选择 id ,尝试将其添加到查询中:

$sql = "SELECT id,asset_type,department,cost FROM track_data";

相关问题