javascript - 编辑php内循环的特定记录

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

目前我已经编码了

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

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

}
  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 (数据) {alert("已保存"); $("#formDiv").hide() });});
  • 显示结果
  • 关闭弹出窗口

关于javascript - 编辑php内循环的特定记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32198028/

相关文章:

javascript - onchange javascript动态更改链接

PHP/MySQL 与 CSV 数组连接

javascript - 如何默认使下拉字段变灰?

javascript - 拖放后保留 iframe 中的内容 - TinyMCE

javascript - 将垂直滚动转换为水平 div 位置 jQuery

javascript - 防止在 asp.net 中重新加载脚本

php - 将数组中的表单值插入数据库

javascript - 如何更改js中已由php设置的cookie?

jquery - 删除 jquery 元素 slider 上的细灰色边框

javascript - 如何向 promise 链添加另一个并行 AJAX 调用