如何使用我要编辑的 html 表中行中的数据填充表单中的输入字段
How to fill input fields in form with data from row in html table I want to edit
我有这个HTML表格,其中包含来自MySQL数据库的数据和用于在此表中添加/编辑行的表单。如果我添加新行,一切正常,但是当我想编辑时,我不知道如何在此表单中预填充输入文本字段。我将特定行的 id 保存为每个编辑按钮的 id,但我不知道如何从这个单击的按钮中了解 ID。我应该用PHP还是JS来做?
<table class="table table-striped">
<thead>
<tr>
<th> Poradi</th>
<th> Jmeno</th>
<th> Prijmeni</th>
<th> Adresa</th>
<th> Mesto</th>
<th> Telefon</th>
<th> Email</th>
</tr>
</thead>
<tbody>
<?php
$db = new PDO(
"mysql:host=localhost;dbname=xyz",
"xyz",
"xy"
);
$stmp = $db->prepare("SELECT * FROM user");
$stmp->execute();
$data = array();
while ($row = $stmp->fetch()) {
$data[]=($row);
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['login'] . "</td>
<td>" . $row['password'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['surname'] . "</td>
<td>" . $row['address'] . "</td>
<td>" . $row['city'] . "</td>
<td>" . $row['phone'] . "</td>
<td>" . $row['email'] . "</td><td>";
$id = $row['id'];
echo "<a><i class='glyphicon glyphicon-pencil' id=$id></i></a></td>
</tr>";
}
?>
</tbody>
</table>
</div>
<div class="sideMenu">
<form method="post" class="form-group" action="add.php">
<h1>Pridat servis</h1>
<input type="text" name="name" placeholder="Jmeno" required autofocus>
<input type="text" name="surname" placeholder="Prijmeni" required>
<input type="text" name="address" placeholder="Adresa" required>
<input type="text" name="city" placeholder="Mesto" required>
<input type="tel" name="phone" placeholder="Telefon" required>
<input type="email" name="email" placeholder="Email" required>
<div class="btn-toolbar">
<button class="btn" type="submit">Ulozit</button>
<button class="btn" type="button">Zrusit</button>
</div>
</form>
<div class="icon-close">
<img src="http://s3.amazonaws.com/codecademy-content/courses/ltp2/img/uber/close.png">
</div>
</div>
当我
开始时,我在同一页面上。
你应该使用
echo "<a href='"edit.php?id=".$row['id']."'"><i class='glyphicon glyphicon-pencil' id=$id></i></a>
在编辑时.php您将使用 $_GET['id'] 来获取点击元素的 id。
您需要告诉您要编辑哪个记录的脚本。
最好的方法是将 href 添加到您的编辑锚点。
echo "<a href="/url/to/this/script?id=$id"><i class='glyphicon glyphicon-pencil' id=$id></i></a></td>
现在,刷新后,您可以通过以下方式获取要编辑的记录的 id:
// this way provide some base security
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
// Now get record from database (only sample call - need to build prepare statement)
$record = $database->getRecordById($id);
// After that fill form with record data
<input type="text" name="name" placeholder="Jmeno" value="<?php $record->name ?>" required autofocus>
我找到了我一直在寻找的解决方案。这里的人的建议也是正确的,以防万一你从你的网站重定向。
如果可能的话,我想用jQuery动态地做到这一点,我发现这个解决方案非常适合我,所以我在这里分享它。
jQuery:通过单击按钮获取表行的内容
相关文章:
- 如何在Highchart中动态填充数据
- 使用jquery在选择框中的选项中填充数据
- 使用jquery、javascript重新填充数据
- 如何从 API 填充数据并加载到下拉列表中
- 通过遍历JSON文件来填充数据列表选项
- 在引导模式中,点击相应的datatables行中的按钮,填充数据
- 当选择District时,PHP将从数据库中填充数据
- 从角度工厂返回数据,返回对象后填充数据
- 高图表 - 从数组填充数据
- 在承诺函数从 pouchdb 填充数据之前触发 Ionic/Angular $watch的问题
- 无法用同位素填充数据
- 使用 php 发送 json 填充数据表
- JQuery - 在表单中填充数据的正确方法
- 翡翠选择字段填充数据
- C# MVC 填充数据表中的下拉列表组
- 自动填充数据被javascript的onchange事件覆盖
- 如何在html中只使用ng repeat来填充数据,而不使用repeat html's元素
- 用Firebase和Angular填充数据表
- 通过JS Ajax调用问题填充数据
- 使用两个ajax调用在两个html表中填充数据