[英]Update row on datatable using ID
I have a table in an oracle database that I am showing on a web page. 我在一个网页上显示的oracle数据库中有一个表。 I used bootstrap to style my page and dataTables for pagination and search as well as sorting.
我使用bootstrap来设置我的页面和dataTables的样式以进行分页和搜索以及排序。 I want to update any particular row at anytime using the unique ID column(BID), so I have added an update link next to each row using the foreach loop.
我想使用唯一ID列(BID)随时更新任何特定行,因此我使用foreach循环在每行旁边添加了更新链接。
My problem now is to get the logic to build that functionality to make the update link. 我现在的问题是获得构建该功能以制作更新链接的逻辑。 I want to:
我想要:
Find a way to know which row the user has clicked to update, and retrieve that record/row to a form for update using the ID. 找到一种方法来了解用户单击要更新的行,并使用该ID将该记录/行检索到表单以进行更新。
Challenge: I am using a loop to fill the table and I can't think of a way to link each row ID to the update link by it. 挑战:我使用循环填充表格,我想不出一种方法将每个行ID链接到更新链接。 I tried filling an array with the ID's but how to connect what update link to what ID for retrieval beats me.
我尝试使用ID填充数组,但是如何将更新链接连接到用于检索的ID。
I am using html and PHP as well as some simple javascript. 我使用的是HTML和PHP以及一些简单的javascript。 I am not good at javascript and have little knowledge in ajax also.
我不擅长javascript,也对ajax知之甚少。 I am yet to learn them but I understand they are the best to use for such things.
我还没有学习它们,但我知道它们最适合用于这些事情。 Perhaps, I am not using the best approach, so if anybody can help me out with a much better one within my scope.
也许,我没有使用最好的方法,所以如果有人能帮助我在我的范围内找到更好的方法。 Find my code below.
在下面找到我的代码。
<table class="table table-striped" id="scriptstable">
<thead>
<tr>
<th>Update</th><!--This is where update links are-->
<th>Place</th>
<th>Report Name</th>
<th>Index</th>
<th>Source</th>
<th>Core Field</th>
<th>Description</th>
<th>ID</th>
</tr>
</thead>
<?php
//Connection string is here
$stid = oci_parse($conn, 'SELECT * FROM mytable ORDER BY REPORT_NAME');
oci_execute($stid);
echo "<tbody>";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{
echo "<tr>";
echo " <td><a data-toggle='modal' data-target='#myModal' href='#' >Update</a>";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>";
} $bid[]=$row['BID'];//Array that stores ID as they come
echo "</tr>";
}
?>
</tbody>
</table>
UPDATE: 更新:
$ajaxAction = $_REQUEST['ajaxaction'];
if (!method_exists('ajaxHandler', $ajaxAction)) {
die("No such action {$ajaxAction}");
}
$handler = new ajaxHandler();
$handler->$ajaxAction();
class ajaxHandler
{
function __construct()
{
//just an empty constructor
}
function updateRow()
{
//Connection
$conn = oci_connect('username', 'password', 'localhost/XE', 'WE8MSWIN1252');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$BID = $_REQUEST['BID'];
$stid = oci_parse($conn, 'SELECT * FROM Bo_repository WHERE BID = {$BID}');
oci_execute($stid);
$row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);
// echo " <td><a id='{$row['BID']}' data-toggle='modal' data-target='#myModal' href='#' onclick='updateRow(this)'>Update</a></td>";
echo " <td><a id='{$row['BID']}' href='#' onclick='updateRow(this)'>Update</a></td>";
//header("Location:index.php");
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>";
}
}
}
?>
When you emit your rows in the table you can assign the id to the table row... I usually do it concat with row or something... and include the id as the id of your tag. 当您在表中发出行时,您可以将id分配给表行...我通常使用行或其他内容进行连接...并将id作为标记的id包含在内。 You can then use the onclick to call a javascript function using ajax to update your table row dynamically eg
然后,您可以使用onclick使用ajax调用javascript函数来动态更新表行,例如
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{
echo "<tr id='row_{$row['BID']}'>";
echo " <td><a id='{$row['BID']}' data-toggle='modal' data-target='#myModal' href='#' onclick='updateRow(this)'>Update</a></td>";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>";
}
$bid[]=$row['BID'];//not sure this is really needed anymore
echo "</tr>";
}
The updateRow function would be something like this... In a script tag of course... updateRow函数将是这样的...当然在脚本标记中...
function updateRow(element) {
var id = jQuery(element).prop('id');
var url = 'your_back_end.php_file_that_handles_Ajax';//e.g. AjaxHandler.php
var postArg = {};
postArg['BID'] = id;
postArg['ajaxaction'] = 'updateRow';
jQuery.ajax({
url: url,
type: "post",
data: postArg,
success: function (response) {
jQuery('#row_' + id).html(response);
},
error: function(response){
console.log(response);
}
});
}
Your backend file would be pretty simple... I create a class called AjaxHandler and pass all ajax calls to the class for whatever processing I need to do... 你的后端文件非常简单......我创建了一个名为AjaxHandler的类,并将所有ajax调用传递给类,以便进行我需要做的任何处理...
Your file could be something like this example... 你的文件可能就像这个例子......
AjaxHandler.php AjaxHandler.php
<?
$ajaxAction = $_REQUEST['ajaxaction'];
if (!method_exists('ajaxHandler', $ajaxAction)) {
die("No such action {$ajaxAction}");
}
$handler = new ajaxHandler();
$handler->$ajaxAction();
class ajaxHandler
{
function __construct()
{
//just an empty constructor
}
function updateRow()
{
$BID = $_REQUEST['BID'];
$stid = oci_parse($conn, 'SELECT * FROM mytable WHERE BID = {$BID}');
oci_execute($stid);
$row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);
echo " <td><a id='{$row['BID']}' data-toggle='modal' data-target='#myModal' href='#' onclick='updateRow(this)'>Update</a></td>";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>";
}
}
}
This is a very basic async dynamic update using php and ajax... Hope this helps... 这是一个使用php和ajax的非常基本的异步动态更新...希望这有助于......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.