douhao7889 2016-10-07 03:05
浏览 237
已采纳

传递href onclick变量以在PHP中运行

I am trying to pass variables stored in href links to a function. Im able to define the variables from the query results. My problem is passing it to the function once the hyperlink is clicked. This is my code:

<?php
    foreach($pdo->query('SELECT * FROM sk_courses ORDER BY courseID') as $row)
    {
      echo "<a href='#' onclick='hrefClick(".$row['courseID'].");'/>".$row['courseID']."</a><br>";
    } 
?>

This is the function:

<script> 
function hrefClick($course){
      $newCourse=$course;
}
</script>
  • 写回答

3条回答 默认 最新

  • dongluolie3487 2016-10-14 14:38
    关注

    I was able to find a way to get it done? It seems pretty straightforward. This is what I came up with.

    <?php
        foreach($pdo->query('SELECT * FROM sk_courses ORDER BY courseID') as $row){
          echo  "<form name='course".$row['courseID']."' action='index.php' method='GET'/>";
          echo "<a href='javascript: document.course".$row['courseID'].".submit();'/>".$row['courseID']."</a>";
          echo "<input type='hidden' name='courseID' value='".$row['courseID']."'/><br>";
          echo "</form>";
        } 
      ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站