简体   繁体   English

在php循环内的锚标记中传递php变量

[英]pass a php variable in an anchor tag inside a php loop

I have a list of records on a table which look like this.. 我在表上有一个记录列表,看起来像这样。

PHP/HTML CODE: PHP / HTML代码:

<script src="js/jquery-3.2.1.js"></script>
<?php
while($resrecord=mysqli_fetch_array($resdata))
{  ?>      
  <tr>
        <td> <?php echo $resrecord[0];?>  </td>
        <td> <?php echo $resrecord[2];?>  </td>
        <td> <?php echo $resrecord[3];?>   </td>
        <td> <a onclick="window.open('test.php?id='+'<?php echo $resrecord[0]; ?>','popup','width=600,height=600');">Tesing</a>  </td>
          //also checked with href of anchor tag
  </tr>
 <?php }   ?>

Which means I can't access a php variable in javascript. 这意味着我无法在javascript中访问php变量。 I also tried these methods 我也尝试过这些方法

<script type="text/javascript">
  var x= '<?php echo $resrecord[0]; ?>';  
  //OR
  var x= '<?php echo json_encode($resrecord[0]); ?>';
    //here x won't populate with above php variables
</script>

I've also tested the above javascript methods on a simple PHP variable which didn't belong to any loop and still didn't work. 我还在一个不属于任何循环且仍然无法正常工作的简单PHP变量上测试了上述javascript方法。

In order to access your variable value in JS, the best way is, keep you value in some element bonded with some selector (keep it hidden if not required to display). 为了访问JS中的变量值,最好的方法是将您的值保留在与某些选择器绑定的某个元素中(如果不需要显示,请使其隐藏)。 Then use that selector to access the value. 然后使用该选择器访问值。 eg. 例如。

<script src="js/jquery-3.2.1.js"></script>
<?php
$i=0;
while($resrecord=mysqli_fetch_array($resdata))
{  $i++ ?>      
  <tr>
        <td><div class="hidden selector_<?php echo $i ?>">
               <?php echo $resrecord[0];?> 
            </div>
               <?php echo $resrecord[0];?> 
        </td>
        <td> <?php echo $resrecord[2];?>  </td>
        <td> <?php echo $resrecord[3];?>   </td>
        <td> <a onclick="window.open('test.php?id='+'<?php echo $resrecord[0]; ?>','popup','width=600,height=600');">Tesing</a>  </td>
          //also checked with href of anchor tag
  </tr>
 <?php }   ?>

JAVASCRIPT JAVASCRIPT

var x = $(".selector_1").val();

// you can iterate if required; //您可以根据需要进行迭代; Hope you may get some idea with above example 希望您可以从上述示例中获得一些想法

[UPDATED] You can pass the value using attribute in a class, Try this one. [更新]您可以在类中使用using属性传递值,请尝试使用此属性。

<script src="js/jquery-3.2.1.js"></script>
<?php while($resrecord=mysqli_fetch_array($resdata))  {  ?>      
    <tr>
         <td> <?php echo $resrecord[0];?>  </td>
         <td> <?php echo $resrecord[2];?>  </td>
         <td> <?php echo $resrecord[3];?>   </td>
         <td> <a class="getAllData" data-id="<?php echo $resrecord[0];?>">Tesing</a> </td>
         //also checked with href of anchor tag
    </tr>
<?php }   ?>

Then use jquery function: 然后使用jquery函数:

you do this. 你做这个。

<script>
    $(window).ready(function(){
        $(".getAllData").on("click", function(){
            var id = $(this).data('id'); //get the current data-id you the row
            window.open('test.php?id='+id,'popup','width=600,height=600');
        });
    });
</script>

This should do it. 这应该做。

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

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