2

A number of employees and their info gets shown on a page. The info of employees gets retrieved via a DB and then a foreach() loop is used to display all employees who fit the search criteria, an example can be seen on image below

enter image description here

Now when the user clicks the button, a simple Bootstrap pop up modal gets triggered, with some basic form fields. As can be seen from example image below

enter image description here

My Problem

I need to get the $userID when a button is clicked to work with / process data in modal.

Below is an extract of relevant code:

$teacherClass = new TeacherSearch();
            $teachers = $teacherClass->showAllTeachers();
            if (is_array($teachers)) {
            foreach ($teachers as $teacher) {
                 $src = $teacher['userID'];
<div class="teacher-info">
                        <p class="teacherLabel">
                            NAME:
                            <?php
                            echo $teacher['name'];
                            ?>
                        </p>

                        <p class="teacherLabel">
                            HEADLINE:
                            <?php
                            echo $teacher['headline'];
                            ?>

                        <p class="teacherLabel">
                            LOCATION:
                            <?php
                            echo $teacher['location']
                            ?>
                        </p>
                       <!--BUTTON GOES HERE-->
                        }//foreach

What I've tried

Ive tried using an <a> element binding a parameter with userID to it, like so:

<a href="body.php?teacher='<?php echo $teacher['userID'] ?>'" data-target="#myModal">Hire <?php echo $teacher['name'] ?></a>

As is to be expected the following triggered a new page reload inside the modal.

I then tried using a # sign for ahref attribute and then biding parameter $userID to it like so:

 <a href="#?teacher='<?php echo $teacher['userID'] ?>'"></a>

The above lead to an undefined index error as can be seen in the picture above.

Conclusion

I hope this question makes sense, I'm pretty much out of ideas, no idea how to further approach this problem.

WebDevBooster
  • 14,674
  • 9
  • 66
  • 70
Timothy Coetzee
  • 5,626
  • 9
  • 34
  • 97

2 Answers2

2

You add the user-id in the anchor tag.To get the contents of the attribute data-id you have to use

<a id="myid" data-id="123">link</a>
<script>
  $('#myid').data("id");
</script>
Anand Pandey
  • 2,025
  • 3
  • 20
  • 39
0

use javascript function to get userID and show modal:

<a  onclick="myfunction('<?php echo $userID; ?>')>Hire <?php echo $teacher['name'] ?></a>

javascript function:

var userID; // global
function myfunction(id){
    userID = id;
    $("#myModal").modal("show");
    //do somethings with user id here 
}
aidinMC
  • 1,415
  • 3
  • 18
  • 35