[英]how to insert data into a dynamically created row in a table (html) from database using php and mysqli
我有一个充满记录的数据库。 我试图获取该数据并将其显示在搜索特定名称时动态创建的表中。
我的html代码是
<form class="formm" method="POST" action="">
<div class="col-lg-3 col-md-6 col-sm-12 col-xs-12">
<input type="text" name="name" id="nameee" placeholder="Name" />
</div>
<div class="col-lg-3 col-md-6 col-sm-12 col-xs-12">
<input type="text" name="vNum" id="vNum" placeholder="Voucher Number" onkeypress='return event.charCode >= 48 && event.charCode <= 57' maxlength="7" />
</div>
<div class="col-lg-3 col-md-6 col-sm-12 col-xs-12">
<input type="text" name="cnic" id="cnic" placeholder="CNIC Number - (4444455555556)" onkeypress='return event.charCode >= 48 && event.charCode <= 57' maxlength="13" />
</div>
<div class="col-lg-3 col-md-6 col-sm-12 col-xs-12">
<input type="submit" class="submit" value="button" name="search" id="search" onclick="myFunction()">
</div>
</form>
<div class="row" id="tableDiv">
<div class="col-lg-12">
<table id="Resulttable" class="table table-hover table-mc-light-blue">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Voucher Number</th>
<th>Cnic Number</th>
<th>Phone Number</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
我的PHP代码是
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dany";
$rowc_name;
$rowc_id;
$rowc_cnic;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT c_name, c_cnic, c_id FROM customer";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$rowc_name = $row["c_name"];
$rowc_id = $row["c_id"];
$rowc_cnic = $row["c_cnic"];
echo "<br> id: ". $row["c_id"]. " - Name: ". $row["c_name"]. " " . $row["c_cnic"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
此代码获取数据并按左上角的页面上的原样显示。 问题是:即时通讯无法将其加载到表格单元格上。
我无法编写一个JavaScript函数,该函数在单击“我的表单上的搜索按钮”时会在表格中插入此信息。
我猜测是一个javascript函数,该函数将在单击搜索按钮时触发,并根据在文本框中输入的值从数据库中获取一个条目,并将其显示在表上。
请记住,数据库中可以有多个具有相同名称的条目。
您必须使用ajax来执行此操作。 如果使用jQuery做到这一点非常容易。 例如,您可以运行一个简单的代码:
$("button").click(function(){
$.ajax({url: "yourphpcode.php", success: function(result){
// You can do your work with result
}});
});
您可以从这里看到更多示例
您必须在表内部使用while循环来在表内部显示数据,如下所示:
<table>
<thead>
<tr>
<th>Name</th>
<th>id</th>
<th>password</th>
</tr>
</thead>
<tbody>
<tr>
//use your while loop here //
while($row = mysqli_fetch_assoc($varname){
//close your php tag because table is a html element//
?>
<td>Name</td>
<td>id</td>
<td>password</td>
</tr>
</tbody>
//start your php and end while loop end bracket//
<?php } ?>
此代码将以适当的格式在表格中显示所有数据库数据。 如果您还有另一个PHP页面,则将该PHP包含到当前表页面中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.