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

时间:2016-09-19 10:17:36

标签: 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>

1 个答案:

答案 0 :(得分:1)

您可以直接在员工选择框的选项中构建这种功能(顺便说一下,您的代码中有两个关闭的选择标记)。无论如何,为此,您可以利用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>