Php 从表行填充表单
我在datatable行的末尾添加了一个按钮,用于更新数据库中所选特定行的信息 当我点击更新按钮时,一个表单会弹出一个要更新的相关字段 理想情况下,我希望表单自动填充我选择更新的表行中的信息 代码 表:Php 从表行填充表单,php,jquery,mysql,datatable,Php,Jquery,Mysql,Datatable,我在datatable行的末尾添加了一个按钮,用于更新数据库中所选特定行的信息 当我点击更新按钮时,一个表单会弹出一个要更新的相关字段 理想情况下,我希望表单自动填充我选择更新的表行中的信息 代码 表: <div class="viewalljob tab-pane show active" id="profile" role="tabpanel" aria-labelledby="profile-tab"> <h2>Edit Job Table</h2&g
<div class="viewalljob tab-pane show active" id="profile" role="tabpanel" aria-labelledby="profile-tab">
<h2>Edit Job Table</h2>
<table id="edit-job-table" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th class="job_id">ID</th>
<th class="job_date">Date</th>
<th class="job_company">Company Name</th>
<th class="job_contact">Contact</th>
<th class="job_from">From</th>
<th class="job_to">To</th>
<th class="job_driver nowrap">Driver</th>
<th class="job_income">Income (£)</th>
<th class="job_payment">Payment (£)</th>
<th>Update</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<!--Fetch from Database-->
<!--Connect To Database-->
<?php
$host_name = 'xxx';
$database = 'xxx';
$user_name = 'xxx';
$password = 'xxx';
$conn = mysqli_connect($host_name, $user_name, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT *,AS markup
FROM `table`
GROUP BY id";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
// output data of each row
while($result = mysqli_fetch_assoc($query)) {
echo "<tr>
<td class='job_id'>".$result['id']."</td>
<td class='job_date'>".$result['adddate']."</td>
<td class='job_company'>".$result['customer']."</td>
<td class='job_contact'>".$result['addcontact']."</td>
<td class='nowrap job_from'>".$result['addfrom']."</td>
<td class='nowrap job_to'>".$result['addto']."</td>
<td class='nowrap job_driver'>".$result['adddriver']."</td>
<td class='currency job_income'>".$result['addincome']."</td>
<td class='currency job_payment'>".$result['addpayment']."</td>
<td><button type='button' name='update' id=".$result['id']." class='btn btn-warning btn-xs update updatebtn'>Update</button></td>
<td><button type='button' name='delete' id=".$result['id']." class='btn btn-danger btn-xs delete deletebtn'>Delete</button></td>
</tr>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
</tbody>
</table>
</div>
<div id="contactForm3">
<h1>Edit Job</h1>
<form id="dataForm" name="dataform" method="POST" action="/">
/**** FORM DATA ****/
</form>
</div>
我使用update
添加了更新按钮,我给出了id=“.$result['id']”
希望可以使用它从id
我假设需要连接到数据库表,例如:
<form>
<?php
//Connect to Database ... //
$sql = SELECT * FROM `table`
WHERE ID = ???
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
// output data of each row
while($result = mysqli_fetch_assoc($query)) {
echo "<span>
<label> label1 </label>
<input value = ".$result['column'].">
</span>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
我希望这是正确的,有人能帮助我做到这一点?实际上,您可以使用javascript/jquery实现这一点,从显示的表中获取行元素,从中可以获得每列/输入值,如下所示:
$('.updatebtn').on('click', function(e) {
e.preventDefault();
var row = $(this).closest('tr'); //get table row from displayed table.
var form = $('#contactForm3').fimd('form'); //form to be populate.
//make sure your form input field with same name as the column name.
//now create form_data object with key as same name as column/input name .
var form_data = { 'id' : row.find('.job_id').value(),
'adddate' : row.find('.job_date').value(),
.....
'addpayment' : row.find('.job_payment').value()
}
//Apply loop to populate values in form.
$.each(form_data, function(key, value){
form.find('input[name="'+ key +'"]').val(value);
});
});
然后在表单上使用PHP提交更新数据库记录/行(希望您了解这一点,如果不让我知道,我将更新代码)。您的问题是什么?“我希望这是正确的,有人可以帮助我做到这一点?”不是很清楚。您能具体说明您对哪一部分有问题吗?@Dharman我有一个数据表,每行末尾都有一个更新按钮。更新按钮被赋予了
id=“.result['id']”。
,因此它的id与特定行的id一致。当我单击update时,我希望它用表行中的详细信息填充更新表单,并希望usinid=“.result['id']”“
将帮助我实现这一点好的,那么最后的PHP代码是关于什么的?如果你的问题是关于UI行为,那么为什么在你的帖子中使用PHP代码?@Dharman我添加了PHP,因为我假设我需要使用PHP从数据库中提取当前数据以填充字段谢谢你的回答。我添加了JS来代替我自己的。但是更新表单(#contactform3)没有打开,所以我更改了var form=$('#contactform3')。find('form')代码>到var form=$('#contactForm3')。fadeToggle('form')代码>,因此表单现在打开,但它不会从表中填充。我应该做些什么更改吗?如果我一直显示表单并将其放在表的上方(或下方),我可以通过单击一行来填充表单,使用:$(document).ready(function(){$(“td”,this).on(“click”,function(){var tds=$(this).parents(“tr”).find(“td”);$。each(tds,function(I,v){$($(“.dataForm3输入”)[i]).val($(v.text();});});})
如果我可以在单击“更新”按钮时调整此选项以使表单显示正确的字段信息,请使用您的代码和我在上面粘贴的代码混合计算出来:-)抱歉!抱歉,没有及时回复您。但我只给出了如何在表单中填充值的解决方案。但是,我仍然感谢您的努力rts需要自己寻找解决方案。
$('.updatebtn').on('click', function(e) {
e.preventDefault();
var row = $(this).closest('tr'); //get table row from displayed table.
var form = $('#contactForm3').fimd('form'); //form to be populate.
//make sure your form input field with same name as the column name.
//now create form_data object with key as same name as column/input name .
var form_data = { 'id' : row.find('.job_id').value(),
'adddate' : row.find('.job_date').value(),
.....
'addpayment' : row.find('.job_payment').value()
}
//Apply loop to populate values in form.
$.each(form_data, function(key, value){
form.find('input[name="'+ key +'"]').val(value);
});
});