我試圖通過可點擊的行將表中的數據傳遞給模態文本框。數據已經顯示在表格中。當單擊行時,它顯示模式,但文本框是空白的。表中的數據來自數據庫。將表中的數據從數據庫傳遞到模態
這是代碼:
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) –