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

标签 php jquery mysql sql

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

这也是我的代码。

<!--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>

最佳答案

您可以直接在员工选择框的选项中简单地构建这种功能(顺便说一句,您的代码中有 2 个结束选择标签)。无论如何,要做到这一点,您可以利用 HTML5 data 属性,如下所示:

    <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>

关于php - 选择员工姓名,他/她的 ID 将显示在 PHP 的文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39570688/

相关文章:

php - NSURLSessionUploadTask 上传文件失败

php - 根据 Eloquent 另一个​​表中的列选择十大类别

mysql - 如何在Wampserver中编写存储过程以自动运行

javascript - 如何在浏览器中将 DIV 调整为全屏

mysql - 带条件乘法的sql语句应该怎么写?

php - AJAX 加载从 onclick 调用的 php 脚本时出现问题;

php - MySQL 用选择的数据更新表

php - 使用 HTML 表单从图像数组中删除字符串

javascript - 使用 jquery 更改图像

javascript - 如何在 javascript/jquery 中使用逗号分隔符格式化和取消格式化美元金额?