简体   繁体   English

如何在数据表的td中添加“ href”链接以打开PHP页面

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

I have this server side table that works fine, except it has modals, but I need to open another PHP edit page(not as modal), linked to the row-id selected. 我有一个工作正常的服务器端表,除了它具有模式,但我需要打开另一个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>";

   }
  }
 });

Where the buttons are, I need something like this: 按钮在哪里,我需要这样的东西:

  <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>

Currently it uses this modal info: 当前,它使用以下模式信息:

 $(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");
    }
   });
  });
 });

The fetch.php for the table data looks like this: 表数据的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);

?>

Please assist this novice programmer about how to adjust the code. 请协助该新手程序员调整代码。

Sounds like you just want to convert an jquery ajax edit thing on a modal dialogue into an actual HTML edit page. 听起来您只是想将模态对话框中的jquery ajax编辑内容转换为实际的HTML编辑页面。 Replace all of that jquery with just a link to a new page you create, and on that page just have a form with the stuff in it. 仅使用指向您创建的新页面的链接来替换所有该jquery,并且在该页面上仅具有其中包含内容的表单。 The first thing you'll want is to check if it's a GET or a POST request - if GET, query from the database for the values and display the standard HTML form. 您需要做的第一件事是检查它是GET还是POST请求-如果是GET,则从数据库中查询值并显示标准HTML表单。 If it's a POST, update the database with the new values (after optionally doing some processing). 如果是POST,请使用新值更新数据库(可选地进行一些处理之后)。

Hopefully that's the question you're asking? 希望这是您要问的问题吗? If so, sorry the answer is so broad, but the question is kind of broad too. 如果是这样,很抱歉,答案如此广泛,但问题也很广泛。 Feel free to clarify further if there's a specific problem you're encountering trying to get the above to work. 尝试进一步解决上述问题时,请随时进一步澄清。

Here is how to add the links: 以下是添加链接的方法:

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