使用Javascript Void 0作为HREF传递PHP变量?

时间:2017-05-25 18:22:05

标签: javascript php html

我正在创建一个允许用户编辑用户的页面。该表给出了下面显示的按钮中的行ID。将它直接指向另一个脚本时,它工作正常。但是,我想动态地打开这个程序所做的用户细节。

虽然我不知道在哪里放置$ row以使页面能够访问它?我的研究更多的是关于在我只需要链接它时在两个程序之间传递。

我只想按下按钮,将$ row [' usersid']变量发送到我可以使用的页面。

编辑:我似乎对自己和你们都感到困惑。如果可能的话,我想用PHP保存它。我需要的只是$ row [' usersid']或存储它的变量,目前在PHP中。要在没有刷新的情况下发送到页面。

echo "<td><a href='javascript:void(0);' onClick=".$row['usersid']."' id='showButton'><span class='glyphicon glyphicon-edit'></span></a></td>";

echo "<td><a href='javascript:void(0);'".$row['usersid']."' id='showButton'><span class='glyphicon glyphicon-edit'></span></a></td>";

2 个答案:

答案 0 :(得分:0)

有很多方法可以将页面中的信息传递给另一个,让我列出一些:

  1. 方法
  2. echo "<td><a href='/path_to_another_php_page.php?userId=$row['usersid']' onClick= id='showButton'><span class='glyphicon glyphicon-edit'></span></a></td>";
    

    然后您可以在$_GET['userId']

    中访问此信息
    1. 如果您想使用此userId,仅用于make ajax请求,您可以执行以下操作:
    2. echo "<td><a href='javascript:void(0);' data-user-id='$row['usersid']' onClick= id='showButton'><span class='glyphicon glyphicon-edit'></span></a></td>";
      

      你可以看到我添加了一个data-user-id属性,其中包含来自php的真实userId,所以在你的Javascript中你可以这样做:

      document.getElementById("showButton").addEventListener("click", function(){
      var userId = this.getAttribute("data-user-id");
      // here you can call your ajax ...
      }, false);
      

      我希望这对你有用。

答案 1 :(得分:0)

如果您想检查您的价值,请使用您获得价值的代码

您也可以使用此代码获取数据

  <?php 
      $row['usersid'] = 10; // fot test case 
      $row['usersid1'] = 13; // fot test case 
      ?>
        <script type="text/javascript">
        $(document).on('click', '.getValue', function(){
            // send data to PHP
            var id = this.getAttribute("userId");
            alert(id)
        });

        </script>
    <?php
    echo "<td><a href='javascript:void(0);' class = 'getValue' userId = ".$row['usersid']." id='showButton'>Button</a></td>";
    echo "<br>";
    echo "<td><a href='javascript:void(0);' class = 'getValue' userId = ".$row['usersid1']." id='showButton'>Button</a></td>";

这是第二种选择         

        <script type="text/javascript">
        function getValue(value){
            alert(value)
        }

        </script>
    <?php
    echo "<td><a href='javascript:void(0);' onClick=getValue(".$row['usersid'].") id='showButton'>Button</a></td>";