简体   繁体   中英

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:

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

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). 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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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