luweiqi luweiqi - 6 months ago 95
Javascript Question

Passing value through anchor tag using JS and PHP

This question is a little long so that it would be clear, thanks in advance!

Introduction:



I currently have a Bootstrap Modal which contains an
<iframe>
. The modal is launched from an anchor tag.

However, the link of the
<iframe>
depends on a PHP variable,
$id
which is dynamic as it is queried from MySQL.




Question:



How can I pass the value of
$id
to the
<iframe>
in Bootstrap Modal, through
data-value
or is there any other ways?




Code:




  • Bootstrap Modal






    <!-- Modal content-->
    <div class="modal-content">
    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h2 class="modal-title">Edit Status</h2>
    </div>
    <div class="modal-body">
    <iframe src="https://example.com/page/?id="></iframe>
    </div>
    <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
    </div>




  • Anchor Tag (To launch the modal)



    <a type="button" data-toggle="modal" data-id="<?php $print id; ?>" data-target="#myModal">Open Modal</a>

  • PHP Code (Sets the variable,
    $id
    )



    <?php
    while ($row = mysqli_fetch_row($result1)){
    list($id, $content) = $row;
    ?>

    <tr>
    <td>
    <p>
    <a type="button" data-toggle="modal" data-id="<?php $print id; ?>" data-target="#myModal">Open Modal</a>
    </p>
    <?php
    }
    ?>
    </td>
    </tr>
    </table>






Elaboration:



The
$id
needs to be passed through the
<a>
tag:

<a type="button" data-toggle="modal" data-id="<?php $print id; ?>" data-target="#myModal">Open Modal</a>


So, if the
$id
is
26
, the
<iframe>
should go to:

https://example.com/page/?id=26 // id needs to be 26 as $id is 26

Answer

Why not

<a href="https://example.com/page/?id=<?php $print id; ?>" target="iframeName" 

? or just

<iframe src="https://example.com/page/?id=<?php $print id; ?>"></iframe>