pass value to modal popup using jquery by using data-id attribute in anchor tag

Sridhar G.K picture Sridhar G.K · Dec 5, 2015 · Viewed 18.4k times · Source
<div class="login-group">
<div class="form-group">
    <table cellspacing="0" cellpadding="0" border="0" class="container">
        <?php
        $selectquery = "Select * from tblservices where category_id = 1";
        $qry=mysqli_query($con,$selectquery);
        if($qry)
        {
            $rowcount=mysqli_num_rows($qry);
            if($rowcount>0)
            {
                $countI = 1;
                while($obj=mysqli_fetch_array($qry))
                {
                    if($countI==0)
                    {
                        ?>
                        <tr>
                            <td class="bgimg">
                                <a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
                                    <?php echo $obj["service_name"]; ?>
                                </a>
                            </td>
                            <td>
                                <a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
                                    <img src="<?php echo '../assets/img/'.$obj['service_image']; ?>" alt="" >
                                </a>
                            </td>
                        </tr>
                        <?php
                        $countI = $countI + 1;
                    }
                    else if($countI %2 ==0)
                    {
                        ?>
                        <!--<a href="#" data-id="<?php echo $obj["value1"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">show value</a>-->
                        <tr>
                            <td>
                                <a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
                                    <img src="<?php echo '../assets/img/'.$obj['service_image']; ?>" alt="" >
                                </a>
                            </td>
                            <td style="font-size:20px; text-align: center;" class="bgimg">
                                <a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
                                    <?php echo $obj["service_name"]; ?>
                                </a>
                            </td>
                        </tr>
                        <?php
                        $countI = $countI + 1;
                    }
                    else
                    {
                        ?>
                        <tr>
                            <td style="font-size:20px; text-align: center;" class="bgimg">
                                <a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
                                    <?php echo $obj["service_name"]; ?>
                                </a>
                            </td>
                            <td>
                                <a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
                                    <img src="<?php echo '../assets/img/'.$obj['service_image']; ?>" alt="" >
                                </a>
                            </td>
                        </tr>
                        <?php
                        $countI = $countI + 1;
                    }
                }
            }
        }
        ?>
    </table>
</div>

the above code displays the values from the database, with the data-id as service id in the anchor tag

and this data-id should be displayed in the modal popup, find below

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="padding-top: 150px;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Choose your slot</h4>
            </div>
            <div class="modal-body">
                <input type="text" name="service_id" class="hiddenid"/>
                <div class="form-group">
                    <table align="center">
                        <tr>
                            <td colspan="2"> <p class="white_text">Preferred slot 1</p></td>
                        </tr>
                        <tr>
                            <td style="padding-right: 10px;"><input type="date" id="theDate" name="slot1_dt" required> </td>
                            <td><input type="time" id="timePicker1" name="slot1_tm" required> </td>
                        </tr>
                        <tr>
                            <td colspan="2" style="padding-top: 15px;"><p class="white_text">Preferred slot 2</p></td>
                        </tr>
                        <tr>
                            <td style="padding-right: 10px;"><input type="date" id="theTomorrow" name="slot2_dt" required> </td>
                            <td><input type="time" id="timePicker2" name="slot2_tm" required> </td>
                        </tr>
                        <tr>
                            <td colspan="2" style="padding-top: 15px;">
                                <center><button type="submit" name="book" class="btn btn-default" value="book" style="font-size: 14px !important;">Book</button>
                                &nbsp;
                                <button type="button" class="btn btn-default" style="font-size: 14px !important;" data-dismiss="modal">Close</button></center>
                            </td>
                        </tr>
                    </table>

                    <?php
                    if(isset($_REQUEST["book"]))
                    {
                        if($_REQUEST["book"])
                        {
                            $service_id=$_REQUEST["service_id"];
                            $customer_id=$_REQUEST["cust_id"];
                            $slot1_dt=$_REQUEST["slot1_dt"];
                            $slot2_dt=$_REQUEST["slot2_dt"];
                            $slot1_tm=$_REQUEST["slot1_tm"];
                            $slot2_tm=$_REQUEST["slot2_tm"];
                            $slot1=$slot1_dt." ".$slot1_tm;
                            $slot1 = date("Y-m-d H:i:s",strtotime($slot1));
                            $slot2=$slot2_dt." ".$slot2_tm;
                            $slot2 = date("Y-m-d H:i:s",strtotime($slot2));
                            $insertqry="INSERT INTO `tblappointments`(`customer_id`, `service_id`, `preferred_slot1_date`, `preferred_slot2_date`)
                            VALUES ('$customer_id','$service_id','$slot1','$slot2')";
                            $res=mysqli_query($con, $insertqry) or die(mysqli_error($con));

                            if($res)
                            {
                                echo("successful.....");
                            }
                        }
                    }
                    ?>
            </div>
        </div>
        <div class="modal-footer"></div>
    </div>
</div>
</div>

and the jquery used for value passing is

<?
$(".modalLink").click(function () {
var passedID = $(this).data('id');
$('#id').val(passedID);

//modifies input in modal
$(".modal-body .hiddenid").val(passedID);});
?>

these codes are under single php file and js is stored as separate file

and on ouput i can't able to get the data-id value to be passed

the text field still remains null.

anyone please help me.

Answer

Nana Partykar picture Nana Partykar · Dec 5, 2015

Firstly, Remove href="#" with href="#myModal".

<a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
    <img src="<?php echo '../assets/img/'.$obj['service_image']; ?>" alt="" >
</a>

Secondly, this is not the way to call data-id. Remove var passedID = $(this).data('id'); with var dataId=$(this).attr('data-id'); Like below.

<?
$(".modalLink").click(function () {
var passedID=$(this).attr('data-id');
.
.
?>

If it works, well and good. If not, follow my code step by step, it will work.

So, i will suggest you to make one page for modal only (somepage.php). Where you pass 'dataId' in proper manner using ajax.

1) Write your <a></a> tag as below. href="#myModal" is mandatory.

<a class="modalLink" href="#myModal" data-toggle="modal" data-id="<?php echo $obj["service_id"]; ?>">
    <?php echo $obj["service_name"]; ?>
</a>

2) In footer, Place this code. (like footer.php)

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="padding-top: 150px;">
    <div class="modal-dialog">
        <div class="modal-content">

        </div>
    </div>
</div>

3) Call your 'somepage.php' (Separate page.Where modal-body is present) through ajax. Place this <script></script> in your JS file.

<script>
$('.modalLink').click(function(){
    var dataId=$(this).attr('data-id');
    $.ajax({url:"somepage.php?dataId="+dataId,cache:false,success:function(result){
        $(".modal-content").html(result);
    }});
});
</script>

4) Create somepage.php (If you want to change this page name. Change in <script></script> too. Both are related.)

somepage.php

<?

// Access $dataId like this way and use any where you want in this modal. 

$dataId=$_GET['dataId'];
?>


<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title" id="myModalLabel">Choose your slot</h4>
</div>
<div class="modal-body">
    <input type="text" name="service_id" class="hiddenid" value="<?echo $dataId;?>"/>
    <div class="form-group">
        <table align="center">
            <tr>
                <td colspan="2"> <p class="white_text">Preferred slot 1</p></td>
            </tr>
            <tr>
                <td style="padding-right: 10px;"><input type="date" id="theDate" name="slot1_dt" required> </td>
                <td><input type="time" id="timePicker1" name="slot1_tm" required> </td>
            </tr>
            <tr>
                <td colspan="2" style="padding-top: 15px;"><p class="white_text">Preferred slot 2</p></td>
            </tr>
            <tr>
                <td style="padding-right: 10px;"><input type="date" id="theTomorrow" name="slot2_dt" required> </td>
                <td><input type="time" id="timePicker2" name="slot2_tm" required> </td>
            </tr>
            <tr>
                <td colspan="2" style="padding-top: 15px;">
                    <center><button type="submit" name="book" class="btn btn-default" value="book" style="font-size: 14px !important;">Book</button>
                    &nbsp;
                    <button type="button" class="btn btn-default" style="font-size: 14px !important;" data-dismiss="modal">Close</button></center>
                </td>
            </tr>
        </table>

        <?php
        if(isset($_REQUEST["book"]))
        {
            if($_REQUEST["book"])
            {
                $service_id=$_REQUEST["service_id"];
                $customer_id=$_REQUEST["cust_id"];
                $slot1_dt=$_REQUEST["slot1_dt"];
                $slot2_dt=$_REQUEST["slot2_dt"];
                $slot1_tm=$_REQUEST["slot1_tm"];
                $slot2_tm=$_REQUEST["slot2_tm"];
                $slot1=$slot1_dt." ".$slot1_tm;
                $slot1 = date("Y-m-d H:i:s",strtotime($slot1));
                $slot2=$slot2_dt." ".$slot2_tm;
                $slot2 = date("Y-m-d H:i:s",strtotime($slot2));
                $insertqry="INSERT INTO `tblappointments`(`customer_id`, `service_id`, `preferred_slot1_date`, `preferred_slot2_date`)
                VALUES ('$customer_id','$service_id','$slot1','$slot2')";
                $res=mysqli_query($con, $insertqry) or die(mysqli_error($con));

                if($res)
                {
                    echo("successful.....");
                }
            }
        }
        ?>
    </div>
</div>
<div class="modal-footer">

</div>

Enjoy Coding.

UPDATED CODE (On @Sridhar's Demand)

1) Change href="#" to href="#myModal" everywhere it's present in your code for modal pop up.

<td class="bgimg">
  <a href="#myModal" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
      <?php echo $obj["service_name"]; ?>
  </a>
</td>

2) Use this code in JS.

JS

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$('.modalLink').click(function(){
  var ID=$(this).attr('data-id');
  $.ajax({url:"NewPage.php?ID="+ID,cache:false,success:function(result){
      $(".modal-content").html(result);
  }});
});
</script>

3) Place this code in footer.

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="padding-top: 150px;">
  <div class="modal-dialog">
    <div class="modal-content">

        </div>
    </div>
</div>

4) Create NewPage.php (If you want to change page name of this page. Please change in Point-2 <script></script> tag too. Both are related)

NewPage.php

Access ID through _GET and do whatever you want with this ID

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title" id="myModalLabel">Choose your slot</h4>
</div>
<div class="modal-body">
  <?php echo $_GET['ID'];?>
</div>
<div class="modal-footer"></div>