【问题标题】:Edit specific record that inside php looped编辑php内部循环的特定记录
【发布时间】:2015-11-18 19:19:11
【问题描述】:

目前我已经像

一样编码了
<div class="row">
 <div class="col-lg-12">
  <table id="usertable" class="table table-bordered table-hover text-center">
   <thead>
    <th class="col-lg-1 text-center">User ID</th>
    <th class="col-lg-4 text-center">Username</th>
    <th class="col-lg-4 text-center">Password</th>
    <th class="col-lg-2 text-center">Role</th>
   </thead>
   <tbody>
   <?php
    require('dbconnectmssql.php');
    $sql = "select [User_ID],[user_decoded],[pass_decoded],[permission] from [rfttest].[dbo].[users]";
    $query = sqlsrv_query ($conn , $sql);
    if($query === false)
    {die( print_r( sqlsrv_errors(), true));}
    while($row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_NUMERIC))
    {
     echo "<tr>";
     foreach($row as $x => $a)
     {
      echo "<td>".$a."</td>";
     }
      echo "<td>";
      echo "<a href='#' ><span class='glyphicon glyphicon-wrench' aria-hidden='true'></span></a>";
      echo "<a href='usercontrol.php?del=$row[0]'>";
      echo "<span class='glyphicon glyphicon-trash' aria-hidden='true'></span>";
      echo "</a>";
      echo "</td>";
      echo "</tr>";
     }
     ?>
    </tbody>
   /table>
  </div>
 </div>

这都是关于查询并显示表中有多少用户。

  • 我已经通过链接到 PHP 文件和 $_GET 数据完成了删除记录(如果您有其他建议的解决方案,以后可能会改进)

我想做的另一件事是

  • 编辑特定记录而不重定向到另一个页面

我现在的想法是单击编辑后弹出的隐藏表单,例如我单击然后弹出带有存储的数据记录的表单,用户可以编辑它并通过提交到 PHP 文件来保存它,然后重定向回这个页面

类似:Contain form within a bootstrap popover? 帖子

<a href="#" id="popover">the popover link</a>
<div id="popover-head" class="hide">
  some title
</div>
<div id="popover-content" class="hide">
  <!-- MyForm -->
</div>

但我还是想不通

  • 如何编写代码以使链接打开特定的 popover-head/content 可能 popover-head/content 必须包含在 id 或其他内容中?
  • PHP 怎么样
  • JS/JQ 呢

任何解决方案我只想要一些东西来编辑回显的特定记录

对不起我的英语不好

谢谢

【问题讨论】:

  • 研究 AJAX 来做到这一点
  • 你能给我一些关于 AJAX 的例子吗,因为我没有 AJAX 经验:(

标签: javascript php jquery sql-server twitter-bootstrap-3


【解决方案1】:

您可能希望为此使用 AJAX。

这对我来说看起来很痛苦 - 如果它起作用,谷歌蜘蛛会删除页面上的所有用户:

}
  echo "<td>";
  echo "<a href='#' ><span class='glyphicon glyphicon-wrench' aria-hidden='true'></span></a>";
  echo "<a href='usercontrol.php?del=$row[0]'>";
  echo "<span class='glyphicon glyphicon-trash' aria-hidden='true'></span>";
  echo "</a>";
  echo "</td>";
  echo "</tr>";
 }

可以写

} ?>
 <td>
 <a href='#' ><span class='glyphicon glyphicon-wrench' aria-hidden='true'></span></a>
 <a class="del" href='#' data-userid='<?PHP echo row["User_ID"]; ?>'>
 <span class='glyphicon glyphicon-trash' aria-hidden='true'></span>
 </a>
</td>
</tr>
<?PHP } ?>

然后

$(function() {
  $(".del").on("click",function(e) {
    e.preventDefault(); stop the page from reloading
    var id=$(this).data("userid");
    $.get("usercontrol.php?del="+id,function() {
      alert(id + "deleted");
    });
  });
});

编辑

  • 使用.show().hide() 弹出一个包含数据的表单
  • 更改数据
  • AJAX 提交时的更改 - 请务必使用 preventDefault 来 停止实际提交$("form").on("submit",function(e) { e.preventDefault(); $.post("save.php",$(this).serialize(),function(data) { alert("saved"); $("#formDiv").hide() });});
  • 显示结果
  • 关闭弹出窗口

【讨论】:

  • 感谢删除记录建议 // 但是编辑特定记录呢?
  • 查看更新。您应该能够像使用 data-userid 一样使用链接中的数据填写表格
  • 我正在尝试将其应用到我的项目中,请稍等。也许几个小时大声笑。我有一个问题是否有必要使用 [user_id] 而不是 [0] ?
  • 我不做 PHP。你需要有data-userid="whatever will identify your user enough to delete/edit him/her"
  • 可能是个愚蠢的问题(因为这是我第一次使用 JQ/AJAX 我不知道如何编写 save.php 文件(它不像删除你告诉我的那样。通过链接获取什么)有区别)。有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 2011-09-01
  • 1970-01-01
  • 2013-06-18
  • 2021-02-09
  • 1970-01-01
  • 1970-01-01
  • 2020-09-15
  • 2019-09-25
相关资源
最近更新 更多