使用JS和PHP通过锚标记传递值


Passing value through anchor tag using JS and PHP

这个问题有点长,所以会很清楚,提前谢谢

简介:

我目前有一个包含<iframe>的Bootstrap Modal。模态是从锚点标签启动的。

然而,<iframe>的链接依赖于一个PHP变量$id,该变量在从MySQL查询时是动态的。


问题:

如何通过data-value$id的值传递给Bootstrap Modal中的<iframe>,或者有其他方法吗?


代码:

  • 引导模式

    <!-- 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>
    
  • 锚点标签(启动模态)

    <a type="button" data-toggle="modal" data-id="<?php $print id; ?>" data-target="#myModal">Open Modal</a>
    
  • PHP代码(设置变量$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>
    

细化:

$id需要通过<a>标签:

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

因此,如果$id26,则<iframe>应转到:

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

为什么不

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

或者只是

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

手动更改iframe的src如何?

<a type="button" data-toggle="modal" data-id="<?php $print id; ?>" data-target="#myModal"
onclick="
document.getElementById('iframe-id').src = $(this).data('id');
"
>Open Modal</a>