簡體   English   中英

如何在數據表的td中添加“ href”鏈接以打開PHP頁面

[英]How to add a “href” link in a td of data table to open a PHP page

我有一個工作正常的服務器端表,除了它具有模式,但我需要打開另一個PHP編輯頁面(不是模式),該頁面鏈接到選定的行ID。

 </div>
   <div class="table-responsive">
    <table id="users_data" class="table table-bordered table-striped">
     <thead>
      <tr>
       <th data-column-id="id" data-type="numeric">No</th>
        <th data-column-id="idno">Id No</th>
        <th data-column-id="surname">Surname</th>
        <th data-column-id="firstname">Firstname</th>
       <th data-column-id="category_name">Category</th>
       <th data-column-id="age">Age</th>
       <th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
      </tr>
     </thead>
    </table>

    <script type="text/javascript" language="javascript" >
$(document).ready(function(){
 $('#add_button').click(function(){
  $('#users_form')[0].reset();
  $('.modal-title').text("Add New Users");
  $('#action').val("Add");
  $('#operation').val("Add");
 });

 var usersTable = $('#users_data').bootgrid({
  ajax: true,
  rowSelect: true,
  post: function()
  {
   return{
    id: "b0df282a-0d67-40e5-8558-c9e93b7befed"
   };
  },
  url: "fetch.php",
  formatters: {
   "commands": function(column, row)
   {
return "<button type='button'  class='btn btn-warning btn-xs update' data-row-id='"+row.id+"'>Edit</button>" + 
"&nbsp; <button type='button'  class='btn btn-danger btn-xs delete' data-row-id='"+row.id+"'>Delete</button>";

   }
  }
 });

按鈕在哪里,我需要這樣的東西:

  <td><a href="update2.php?u=<?php echo $row['id'] ?>"><span class="glyphicon glyphicon-pencil" aria-hidden="true" </span><b><font size="3" color="red"</font> Edit<b></a></td>

當前,它使用以下模式信息:

 $(document).on("loaded.rs.jquery.bootgrid", function()
 {
  usersTable.find(".update").on("click", function(event)
  {
   var id = $(this).data("row-id");
    $.ajax({
    //url:"update2.php",    
    url:"fetch_single_entries.php",
    method:"POST",
    data:{id:id},
    dataType:"json",
    success:function(data)
    {
     $('#usersModal').modal('show');



      $('#categories').val(data.categories);
     $('#idno').val(data.idno);
     $('#surname').val(data.surname);
     $('#firstname').val(data.firstname);
     $('#age').val(data.age);
     $('.modal-title').text("Edit User");
     $('#id').val(id);
     $('#action').val("Edit");
     $('#operation').val("Edit");
    }
   });
  });
 });

表數據的fetch.php如下所示:

<?php
//fetch.php
include("connection.php");
$query = '';
$data = array();
$records_per_page = 10;
$start_from = 0;
$current_page_number = 0;
if(isset($_POST["rowCount"]))
{
 $records_per_page = $_POST["rowCount"];
}
else
{
 $records_per_page = 10;
}
if(isset($_POST["current"]))
{
 $current_page_number = $_POST["current"];
}
else
{
 $current_page_number = 1;
}
$start_from = ($current_page_number - 1) * $records_per_page;
$query .= "
 SELECT 
  users.id, 
  tblcategories.category_name, 
  users.idno,users.surname,users.firstname,    
  users.age FROM users 
  INNER JOIN tblcategories 
 ON tblcategories.category_id = users.category_id ";
if(!empty($_POST["searchPhrase"]))
{
 $query .= 'WHERE (users.id LIKE "%'.$_POST["searchPhrase"].'%" ';
 $query .= 'OR tblcategories.category_name LIKE "%'.$_POST["searchPhrase"].'%" ';
 $query .= 'OR users.idno LIKE "%'.$_POST["searchPhrase"].'%" ';
 $query .= 'OR users.surname LIKE "%'.$_POST["searchPhrase"].'%" ';
  $query .= 'OR users.firstname LIKE "%'.$_POST["searchPhrase"].'%" ';
 $query .= 'OR users.age LIKE "%'.$_POST["searchPhrase"].'%" ) ';
}
$order_by = '';
if(isset($_POST["sort"]) && is_array($_POST["sort"]))
{
 foreach($_POST["sort"] as $key => $value)
 {
  $order_by .= " $key $value, ";
 }
}
else
{
 $query .= 'ORDER BY users.id DESC ';
}
if($order_by != '')
{
 $query .= ' ORDER BY ' . substr($order_by, 0, -2);
}

if($records_per_page != -1)
{
 $query .= " LIMIT " . $start_from . ", " . $records_per_page;
}
//echo $query;
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result))
{
 $data[] = $row;
}

$query1 = "SELECT * FROM users";
$result1 = mysqli_query($connection, $query1);
$total_records = mysqli_num_rows($result1);

$output = array(
 'current'  => intval($_POST["current"]),
 'rowCount'  => 10,
 'total'   => intval($total_records),
 'rows'   => $data
);

echo json_encode($output);

?>

請協助該新手程序員調整代碼。

聽起來您只是想將模態對話框中的jquery ajax編輯內容轉換為實際的HTML編輯頁面。 僅使用指向您創建的新頁面的鏈接來替換所有該jquery,並且在該頁面上僅具有其中包含內容的表單。 您需要做的第一件事是檢查它是GET還是POST請求-如果是GET,則從數據庫中查詢值並顯示標准HTML表單。 如果是POST,請使用新值更新數據庫(可選地進行一些處理之后)。

希望這是您要問的問題嗎? 如果是這樣,很抱歉,答案如此廣泛,但問題也很廣泛。 嘗試進一步解決上述問題時,請隨時進一步澄清。

以下是添加鏈接的方法:

return "<a href=\"update2.php?u=" + row.id + "\"><button type=\"button\" class=\"btn btn-xs btn-warning\" data-row-id=\"" + row.id + "\"><span class=\"glyphicon glyphicon-pencil\"></span></button></a> " +
"<a href=\"delete2.php?u=" + row.id + "\"><button type=\"button\" class=\"btn btn-xs btn-danger\" data-row-id=\"" + row.id + "\"><span class=\"glyphicon glyphicon-trash\"></span></button></a>";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM