简体   繁体   English

将表中的user_id传递到另一个页面

[英]Pass user_id in a table to another page

I have a list of users that I'm reporting in a table. 我在表格中有一个要报告的用户列表。 The first column contains the user_id, and I want to pass that to another page that displays the users details. 第一列包含user_id,我想将其传递给另一个显示用户详细信息的页面。 I don't want to pass it through the URL with $_GET. 我不想使用$ _GET通过URL传递它。 How do I do this? 我该怎么做呢? I can use only HTML & PHP. 我只能使用HTML和PHP。 One post I saw seemed like it was on the right track, but I can't get it to work. 我看到的一篇帖子似乎在正确的轨道上,但是我无法使它正常工作。 Here's what I have: 这是我所拥有的:

<h1>Maintain Users</h1>
<div id="ir-report">
    <table class="table">
        <tr class = "table tr">
            <th class="table th">User Id</th>
            <th>Username</th>
            <th>Name</th>
            <th>Email</th>
            <th>Activated?</th>
            <th>Type</th>
            <th>Account Nbr</th>
        </tr>
        <?php while ($row = mysqli_fetch_assoc($query))
        {?>
        <tr class="table tr">
            <td class ="table td" onclick="submit_id($row['user_id'])"><img src="images\editpensil.jpg" alt="UserId" width="25" height="25"></td>
            <td><?php echo $row['username'] ?></td>
            <td><?php echo $row['first_name'] ?></td>
            <td><?php echo $row['email'] ?></td>
            <td><?php echo $row['active'] ?></td>
            <td><?php echo $row['type'] ?></td>
            <td><?php echo $row['acct_nbr'] ?></td>
        </tr>
        <?php } ?>
        </table>
</div>

What I was expecting to happen is that "on click" the function submit_id($var) would be called. 我期望发生的事情是“单击”时将调用函数commit_id($ var)。 To test that this is working, my function is very simple for the moment: 为了测试它是否有效,目前我的功能非常简单:

function submit_id($var) {
    header('Location: index.php');
    exit();
}

But nothing happens when I clime on the edit icon (editpensil.jpg). 但是,当我点击编辑图标(editpensil.jpg)时,什么也不会发生。 The application doesn't appear to try and execute anything. 该应用程序似乎没有尝试执行任何操作。 What am I missing? 我想念什么? -- Thanks. - 谢谢。

  1. You're mixing PHP and JS. 您正在混合使用PHP和JS。 The function called in an onclick handler must be JS, it cannot be php. onclick处理程序中调用的函数必须是JS,不能是php。

  2. What's wrong with passing the id through the URL? 通过URL传递ID有什么问题? That's quite obviously the only simple practical solution. 显然,那是唯一简单实用的解决方案。 Alternatives are: 替代方法是:

    • using cookies, but that's a mess if a user opens multiple tabs/windows on your site, or wants to bookmark the page, or for a reopen after restart of the browser. 使用Cookie,但是如果用户在您的网站上打开多个选项卡/窗口,或者想要为页面添加书签,或者在浏览器重启后重新打开,那就太麻烦了。 And that really doesn't help much. 这确实没有太大帮助。

    • using some kind of other id that points to the user id, but that's just adding another layer for not much. 使用某种其他ID指向用户ID,但这只是增加了一层。

Unless you tell us exactly why you don't want to pass the id through the URL, it will be difficult to suggest anything. 除非您确切告诉我们为什么您不想通过URL传递ID,否则很难提出任何建议。

You can't call a PHP function from an onclick attribute, or any other on attribute. 您不能从onclick属性或任何其他on属性调用PHP函数。 Those functions must be Javascript functions. 这些函数必须是Javascript函数。

If you really need/want to not pass the id through GET, you can wrap the table in a <form> with the action set to the page you want to send it to, with a method of POST and a hidden input field. 如果确实需要/不想通过GET传递ID,则可以将表包装在<form> ,并将action设置为要发送到的页面,并使用POST method和隐藏的输入字段。 I know your question says you can only use HTML and PHP; 我知道您的问题说您只能使用HTML和PHP; without Javascript, the only way you can really do this is to add a button to the form/table. 如果没有Javascript,您真正要做到的唯一方法就是在表单/表格中添加一个按钮。 You could use CSS to style it so it looks like a link, or anything not-button like. 您可以使用CSS设置样式,使其看起来像一个链接或任何非按钮式的东西。

Example without Javascript: 没有Java脚本的示例:

<form action="index.php" method="post">
  <tr class="table tr">
    <td class ="table td">
      <img src="images\editpensil.jpg" alt="UserId" width="25" height="25">
    </td>
    <td><?php echo $row['username'] ?></td>
    <td><?php echo $row['first_name'] ?></td>
    <td><?php echo $row['email'] ?></td>
    <td><?php echo $row['active'] ?></td>
    <td><?php echo $row['type'] ?></td>
    <td><?php echo $row['acct_nbr'] ?></td>
    <td><input type="submit" value="Link to" /></td>
  </tr>
  <input type="hidden" name="user_id" value="<?php echo $row['user_id'] ?>" />
</form>

When that button is clicked, the form will be posted to index.php and you can get to the id through $_POST['user_id'] 单击该按钮后,表单将发布到index.php ,您可以通过$_POST['user_id']来获取ID。


If you are willing and able to use Javascript, you should be able to do the following HTML/PHP: 如果您愿意并且能够使用Javascript,则应该能够执行以下HTML / PHP:

<form action="index.php" method="post" id="user_form_<?php echo $row['user_id'] ?>">
  <tr class="table tr">
    <td class ="table td user_id_link" data-userid="<?php echo $row['user_id'] ?>">
      <img src="images\editpensil.jpg" alt="UserId" width="25" height="25">
    </td>
    <td><?php echo $row['username'] ?></td>
    <td><?php echo $row['first_name'] ?></td>
    <td><?php echo $row['email'] ?></td>
    <td><?php echo $row['active'] ?></td>
    <td><?php echo $row['type'] ?></td>
    <td><?php echo $row['acct_nbr'] ?></td>
  </tr>
  <input type="hidden" name="user_id" value="<?php echo $row['user_id'] ?>" />
</form>

And the following Javascript, although this will only work in IE9+: 和以下Javascript,尽管仅在IE9 +中有效:

var links = document.getElementsByClassName('user_id_link');

for (link in links) {
  link.addEventListener('click', function() {
    var formId = 'user_form_' + this.userid;
    document.getElementById(formId).submit();
  }, false);
}

Or the much cleaner jQuery version, which has better browser compatibility and is really an invaluable toolkit for a web developer: 或者更干净的jQuery版本,它具有更好的浏览器兼容性,对于Web开发人员来说确实是一个宝贵的工具包:

HTML/PHP: HTML / PHP:

<form action="index.php" method="post">
  <tr class="table tr">
    <td class ="table td user-id-link">
      <img src="images\editpensil.jpg" alt="UserId" width="25" height="25">
    </td>
    <td><?php echo $row['username'] ?></td>
    <td><?php echo $row['first_name'] ?></td>
    <td><?php echo $row['email'] ?></td>
    <td><?php echo $row['active'] ?></td>
    <td><?php echo $row['type'] ?></td>
    <td><?php echo $row['acct_nbr'] ?></td>
  </tr>
  <input type="hidden" name="user_id" value="<?php echo $row['user_id'] ?>" />
</form>

Javascript/jQuery: Javascript / jQuery:

$(function() {
  $('.user-id-link').on('click', function() {
     $(this).closest('form').submit();
  });
});

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

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