Francis Vargas Francis Vargas - 2 months ago 4
jQuery Question

Choose employee name and his/her ID will show in the textbox in PHP

Click this to see image.
Hello everyone, I hope someone will help me with this. :( Here's the problem, I want to show the ID of Francis when I click it. Also in the database there is a list of employee name and ID. So if I click Francis or Ivann. In the textbox (the arrow) it will show his/her ID specified only in the employee name. :)


Also here's is my code.

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

Answer

You can simply build that kind of Functionality directly in the Options of your Employee Select Box (By the Way, you had 2 closing Select tags in your code). Anyways, to do this you could leverage the HTML5 data Attribute as show below:

    <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)){
                // YOUR OPTION SHOULD HAVE A VALUE ATTRIBUTE IF YOU WISH TO SAVE THE FORM.
                // CHANGE $row["EmpID"] TO THE APPROPRIATE NAME OF THE FIELD
                // CORRESPONDING TO EMPLOYEE ID.
                echo "<option value='{$row["EmpFName"]}' data-emp-id='{$row["EmpID"]}'>";
                echo $row["EmpFName"] . "</option>";
            }
        ?>
    </select>

Then in Javascript (JQuery used here):

    <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[value="' + val + '"').attr("data-emp-id");
                    empID.val(eID);
                });

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