我试图通过可点击的行将表中的数据传递给模态文本框。数据已经显示在表格中。当单击行时,它显示模式,但文本框是空白的。表中的数据来自数据库。将表中的数据从数据库传递到模态
这是代码:
PHP的:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name = "viewport" content = "width= device-width, initial-scale=1">
<link rel = "stylesheet" href = "bs/css/bootstrap.css">
<script src = "bs/js/jquery.min.js"></script>
<script src = "bs/js/bootstrap.min.js"></script>
<meta charset="utf-8">
</head>
<body>
<div class="container">
<div class="header">
<?php include("header.php"); ?>
</div>
<div class = "navigation">
<?php include("navigator.php"); ?>
</div>
</div>
<center>
<style>
table tr:not(:first-child){
cursor: pointer;
}
table tr:not(:first-child)hover{
background-color:azure;
color:black;
}
</style>
<?php
include("dbConfig.php");
$result = mysqli_query ($con, "SELECT * FROM information");
echo "<table id=table border = 1 width = 50%>
<tr>
<th align = center> Control # </th>
<th> Last Name </th>
<th> First Name </th>
<th> Contact Number </th>
</tr>";
while($row=mysqli_fetch_array($result)){
echo "<form action = method =post>";
echo "<tr data-toggle=modal data-target=#fmodal name = pass type = submit>";
echo "<td id='cn'>".$row['Control_Number']. "</td>";
echo "<td>".$row['Last_Name']. "</td>";
echo "<td>".$row['First_Name']. "</td>";
echo "<td>".$row['Contact_Number']. "</td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
if(isset($_POST['submit'])){
$res = mysqli_query($con, "SELECT * FROM information WHERE Control_Number=$_POST[cn]");
while($rowval = mysqli_fetch_array($res)){
$controlnumber= $rowval['Control_Number'];
$lastname= $rowval['Last_Name'];
$firstname= $rowval['First_Name'];
$contactnumber= $rowval['Contact_Number'];
}
}
?>
<div class = "modal fade" id = "fmodal" role = "dialog">
<div class = "modal-dialog" role = "document">
<div class = "modal-content">
<div class = "modal-header">
<button type = "button" class = "close" data-dismiss = "modal">×</button>
<h4>FORM</h4>
</div>
<div class = "modal-body text-center">
<label>Control Number:</label>
<input class = "input-lg" type = "text" id = "Control_Number" value = '<?php echo $controlnumber; ?>'><br>
<label>Last Name:</label>
<input class = "input-lg" type = "text" id = "Last_Name" value = '<?php echo $lastname; ?>'><br>
<label>First Name:</label>
<input class = "input-lg" type = "text" id = "First_Name" value = '<?php echo $firstname; ?>'><br>
<label>Contact Number:</label>
<input class = "input-lg" type = "text" id = "Contact_Number" value = '<?php echo $contactnumber; ?>'><br>
</div>
</div>
</div>
</div>
</center>
</body>
首先,看看http://bobby-tables.com因为你的应用程序运行在SQL注入的风险很高。使用准备好的语句进行查询,绝不会将用户输入直接放入查询中!对于你的问题:首先定义循环外部的变量,比如'$ controlnumber =“”;' - 如果你想了解更多关于PHP变量范围的信息,请查找'PHP while loop scope'。 – Twinfriends
@MYxx因为我理解你的问题,当点击提交按钮模式时必须弹出一行表格数据,并且必须在表格中显示特定值。但是或者你需要我们'AJAX'。 –
@MYxx这里是'php''mysql''ajax''jquery'的类似例子。点击这个链接,并告诉我是否这是你正在寻找或需要我需要添加更多.. [链接1](https://ibb.co/n4N0fF)[链接2](https://ibb.co/kXYj7v) –