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

时间:2022-05-02 09:05:29

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?

如何通过数据值将$id值传递到引导模式下的


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代码(设置变量$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:

$id需要通过标签传递:

<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:

因此,如果$id为26,则

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

2 个解决方案

#1


2  

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> 

#2


1  

How about changing the src of the iframe manually?

手动更改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>

#1


2  

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> 

#2


1  

How about changing the src of the iframe manually?

手动更改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>