Search code examples
phpfunctionvariableshref

Pass href onclick variable to function in 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>

Solution

  • 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>";
        } 
      ?>