简体   繁体   English

获取HTML表中的ROW ID以在模式jQuery中使用; PHP

[英]Get ROW ID in a HTML Table to use in a modal jQuery; PHP

So this time I have a HTML table and when I click in a anchor that is located in a tag it opens a modal, where we can change a user password. 因此,这一次我有了一个HTML表,当我单击位于标签中的锚点时,它会打开一个模式,我们可以在其中更改用户密码。

But to update the password in the users table I need to get the ID. 但是要更新用户表中的密码,我需要获取ID。 My problem is, how can I get the row ID/user ID in the modal. 我的问题是,如何获取模态中的行ID /用户ID。

My table is created trough a for: 我的表格是通过以下方式创建的:

for ($i = 0; $i < $size; $i++) {
print ("
<tr>
<td>".$Users[$i][0]."</td>
<td>".$Users[$i][1]."</td>
<td>*****</td>
<td><a data-modal-id='delete' href='#' >Change PW</a></td>
</tr>
<tr>");
}

So when I click the anchor the modal opens and I can manage to get the modal to work. 因此,当我单击锚点时,模态会打开,并且我可以设法使模态起作用。 What I need is somehow to get the ID to build the SQL query to update the password. 我需要以某种方式获取ID来构建SQL查询以更新密码。

The modal: 模态:

      <header>
        <h3>Alterar Password</h3>
      </header>

      <div class="modal-body">

        <form action="" method="POST" id="changepw" name="changepw">            
            <label>Nova Password:</label><br/>
            <input class="textbox" type="password" name="newpassword" id="newpassword" /> <br/>

            <input class="btnSubmit" type="submit" name="changepwSub" id="changepwSub" form="changepw" value="Alterar Password" />          
        </form>         
      </div>

      <footer>
        <a href="#" class="js-modal-close"><input class="btnSubmit" type="button" value="Fechar" style="max-width:100px;" /></a>
      </footer>

      <?php

       //The PHP Code to Update it will be here.
        $query = "UPDATE auth SET password=xxx WHERE ID="

       ?>  
    </div>

You simply need 3 steps: 您只需要3个步骤:

  1. As proposed in the comments, embed the user ID in the dataset of your anchor / row / cell / whatever is linked to the click. 如评论中所建议,将用户ID嵌入到锚点/行/单元格/与点击链接相关的数据集中。 Eg <a data-modal-id='delete' data-modal-userid='user_id' href='#' >Change PW</a> 例如<a data-modal-id='delete' data-modal-userid='user_id' href='#' >Change PW</a>
  2. Modify the script that generates the modal to read that data and add a hidden form field. 修改生成模式的脚本以读取该数据并添加一个隐藏的表单字段。 Eg '<input type="hidden" name="userid" value="' + event.target.dataset.modalUserid + '" /> ' 例如'<input type="hidden" name="userid" value="' + event.target.dataset.modalUserid + '" /> '
  3. In your PHP script, simply retrieve that extra field to fill in to your SQL query. 在您的PHP脚本中,只需检索该额外字段即可填写您的SQL查询。

I hope this is a sort of admin board for which you have proper credentials and security. 我希望这是一种具有适当凭据和安全性的管理委员会。 You might need to check on server side that the user has credentials to modify the record of user with that given ID, as anyone could pass a fake ID and modify someone else's record. 您可能需要在服务器端检查用户是否具有凭据来修改具有给定ID的用户记录,因为任何人都可以传递假ID并修改其他人的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM