无法从选项值获取HTML数据

时间:2018-07-18 11:11:27

标签: php mysql ajax

我正在获取所选选项值的ID,但是表数据不会随着选项更改而显示。我没有收到错误,也找不到错误。

dashboard.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内的值。我没有收到任何类型的错误,但没有显示值。我该如何解决这个问题。 enter image description here

1 个答案:

答案 0 :(得分:1)

在第一个SQL查询中:

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

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

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