选择员工姓名,他/她的 ID 将显示在 PHP 的文本框中
Posted
技术标签:
【中文标题】选择员工姓名,他/她的 ID 将显示在 PHP 的文本框中【英文标题】:Choose employee name and his/her ID will show in the textbox in PHP 【发布时间】:2017-01-26 23:55:17 【问题描述】:我想在单击时显示 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" >
<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】:您可以直接在员工选择框的选项中简单地构建这种功能(顺便说一下,您的代码中有 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>
【讨论】:
当我选择一个名字时,仅在该员工姓名中指定的员工 ID 将显示在该文本框中。 感谢您的帮助,但仍然无法正常工作。 :'( 当我选择员工姓名时,文本框中没有值。:( @FrancisVargas 您只需确保将$row['id']
更改为适当的值,并将上面发布的整个“ 与@987654325 一起使用@。这应该可行....
我认为问题在于 $row['id'].我没有看到任何 $row['id']。我该如何改变它? :)
保存员工 ID 的字段(在您的数据库表中)的名称是什么?如果你得到这个名字,只需将它替换为$row['field_name_for_employee_id_in_your_table']
最重要的是;您应该通过查询使该字段可用:` $result=mysqli_query($con, "SELECT EmpFName, id FROM employee");` 请注意,我们现在在 Select 子句中包含了一个新字段:@ 987654327@以上是关于选择员工姓名,他/她的 ID 将显示在 PHP 的文本框中的主要内容,如果未能解决你的问题,请参考以下文章
我可以在其他应用用户的应用上下文中使用应用用户的朋友公共数据(id、姓名、性别)吗
如何正确使用连接/子查询从多个表中选择数据? (PHP-MySQL)