选择员工姓名,他/她的ID将显示在PHP文本框中

选择员工姓名,他/她的ID将显示在PHP文本框中,php,jquery,mysql,sql,Php,Jquery,Mysql,Sql,我想在单击时显示Francis的ID。数据库中还有一个员工姓名和ID的列表。因此,如果我单击Francis或Ivann。在文本框中,箭头将显示仅在员工姓名中指定的ID: 这是我的代码 <!--Create--> <?php include "config.php"; include "header.php"; ?> <?php if(isset($_POST['bts'])): if($_POST['id']!=null &&

我想在单击时显示Francis的ID。数据库中还有一个员工姓名和ID的列表。因此,如果我单击Francis或Ivann。在文本框中,箭头将显示仅在员工姓名中指定的ID:

这是我的代码

<!--Create-->
<?php
  include "config.php";
  include "header.php";
?>
<?php
  if(isset($_POST['bts'])):
    if($_POST['id']!=null && $_POST['en']!=null && $_POST['date']!=null && $_POST['dp']!=null && $_POST['deduc']!=null){
       $stmt = $mysqli->prepare("INSERT INTO personal(id_personal,name,date,datepaid,deduction) VALUES (?,?,?,?,?)");
       $stmt->bind_param('sssss', $id, $en, $date, $dp, $deduc);

       $id = $_POST['id'];
       $en = $_POST['en'];
       $date = $_POST['date'];
       $dp = $_POST['dp'];
       $deduc = $_POST['deduc'];

       if($stmt->execute()):
?>
<p></p>
<div class="alert alert-success alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert"></button>
  <strong>Alright!</strong> Successfully added.
</div>
<?php
  endif;
  } else {
?>
<p></p>
<div class="alert alert-warning alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert"></button>
  <strong>Error!</strong> You must fill all the blanks.
</div>
<?php
}
  endif;
?>
  <p>
</p>
<div class="panel panel-default">
  <div class="panel-body">
  <form role="form" method="post">
    <div class="form-group">
      <label for="id">Employee ID</label>
      <input type="text" class="form-control" name="id" id="id" placeholder="Enter ID"/>
    </div>
    <div class="form-group">
      <label for="en">Employee Names</label>
      <select class="form-control" id="en" name="en">
        <option>Choose</option>
        <?php
          include("alqdb.php");
          $result=mysqli_query($con, "SELECT EmpFName FROM employee");
          while($row=mysqli_fetch_assoc($result)){
            echo "<option>".$row["EmpFName"]."</option>";
          }
        ?>
        </select>
      </select>
    </div>
    <div class="form-group">
      <label for="date">Date</label>
      <input type="date" class="form-control" name="date" id="date"/>
    </div>
    <div class="form-group">
      <label for="dp">Date to be Paid</label>
      <input type="date" class="form-control" name="dp" id="dp"/>
    </div>
    <div class="form-group">
      <label for="sal">Salary</label>
      <input type="text" class="form-control" name="sal" id="sal" placeholder="Salary"/>
    </div>
    <div class="form-group">
      <label for="amnt">Amount</label>
      <input type="text" class="form-control" name="amnt" id="amnt" placeholder="Enter Amount"/>
    </div>
    <div class="form-group">
      <label for="deduc">Deduction</label>
      <input type="text" class="form-control" name="deduc" id="deduc" value="0.00" readonly/>
    </div>
    <button type="submit" name="bts" class="btn btn-default">Ok</button>
  </form>
<?php
  include "footer.php";
?>
<!--Table-->
<?php
  include "config.php";
  include "header.php";
?>
  <p>
</p>
<label>List of Transactions</label>
<table id="ghatable" class="display table table-bordered table-stripe" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Date</th>
      <th>Date to be Paid</th>
      <th>Deduction</th>
      <th>Action</th>
     </tr>
  </thead>
  <tbody>
  <?php
    $res = $mysqli->query("SELECT * FROM personal");
    while ($row = $res->fetch_assoc()):
  ?>
    <tr>
      <td><?php echo $row['id_personal'] ?></td>
      <td><?php echo $row['name'] ?></td>
      <td><?php echo date("F j, Y", strtotime($row['date'])) ?></td>
      <td><?php echo date("F j, Y", strtotime($row['datepaid'])) ?></td>
      <td><?php echo $row['deduction'] ?></td>
      <td>
        <a href="update.php?u=<?php echo $row['id_personal'] ?>"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> Edit</a>
        <a onclick="return confirm('Are you want deleting data')" href="delete.php?d=<?php echo $row['id_personal'] ?>"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</a>
      </td>
    </tr>
  <?php
    endwhile;
  ?>
  </tbody>
</table>
<?php
  include "footer.php";
?>
<script type="text/javascript" src="js/jquery.js"></script>
<script language="javascript">
    $("#amnt, #sal").keyup(function() {
        $("#deduc").val(($("#sal").val() - $("#amnt").val()).toFixed(2));
    });
</script>

您可以直接在员工选择框的选项中构建这种功能。顺便说一下,您的代码中有两个结束选择标记。无论如何,要做到这一点,您可以利用HTML5数据属性,如下所示:

    <select class="form-control" id="en" name="en">
        <option>Choose</option>
        <?php
            include("alqdb.php");

            // ALSO ADD THE ID COLUMN TO YOUR SELECT QUERY SO YOU HAVE IT AVAILABLE TO YOU.
            $result =   mysqli_query($con, "SELECT EmpFName, id FROM employee");

            while($row  = mysqli_fetch_assoc($result)){
                // YOUR OPTION SHOULD HAVE A VALUE ATTRIBUTE IF YOU WISH TO SAVE THE FORM.
                // CHANGE $row["id"] TO THE APPROPRIATE NAME OF THE FIELD
                // CORRESPONDING TO EMPLOYEE ID.
                echo "<option value='{$row["EmpFName"]}' data-emp-id='{$row["id"]}'>";
                echo $row["EmpFName"] . "</option>";
            }
        ?>
    </select>
然后在这里使用的Javascript JQuery中:

    <script type="text/javascript" src="js/jquery.js"></script>
    <script language="javascript">
        (function($) {
            $(document).ready(function(){
                var empName = $("#en");
                var empID   = $("#id");

                empName.on("change", function(evt){
                    var eN  = $(this);          
                    var eID = eN.children('option:selected').attr("data-emp-id");
                    empID.val(eID);
                });

                $("#amnt, #sal").keyup(function() {
                    $("#deduc").val(($("#sal").val() - $("#amnt").val()).toFixed(2));
                });
            });
        })(jQuery);
    </script>

当我选择姓名时,仅在该员工姓名中指定的员工ID将显示在该文本框中。我感谢您的帮助,但它仍然不起作用。:'选择员工姓名时,文本框中没有值:@FrancisVargas您只需确保将$row['id']更改为适当的值,并使用整个。。。上面用data-emp-id贴了出来。这应该行……我认为问题在于$row['id']。我没有看到任何$row['id']。我怎样才能改变它呢DB表中保存员工ID的字段的名称是什么?如果您得到了这个名称,只需将其替换为$row['field_name_for_employee_id_in_your_table'],最重要的是;您应该通过查询使该字段可用:`$result=mysqli\u Query$con,选择EmpFName,id FROM employee;`请注意,我们现在在Select子句中包含了一个新字段:id