dongqiuxu2270
2017-08-18 07:31 阅读 74

如何将PHP内容放入iframe?

I have an iframe and I want to put PHP content in it like in a text-area.

<textarea><?php echo $content; ?></textarea>

But it does not work for an iframe. How can I solve it?

Code:

<form action="php_parsers/article_parser.php" id="writearticle" name="writearticle" method="POST">
    <p style="text-align: center; font-size: 22px;">Create Article</p>
    <textarea name="title" id="title" type="text" maxlength="100" placeholder="Article Title"></textarea>
    <div class="toolbar">
        <a onclick="iBold()"><i class='fa fa-bold'></i></a>
        <a onclick="iUnderline()"><i class='fa fa-underline'></i></a>
        <a onclick="iItalic()"><i class='fa fa-italic'></i></a>
        <a onclick="iFontSize()"><i class='fa fa-font'></i></a>
        <a onclick="iForeColor()"><i class='fa fa-align-left'></i></a>
        <a onclick="iHorizontalRule()"><i class='fa fa-align-center'></i></a>
        <a onclick="iUnorderedList()"><i class='fa fa-list-ul'></i></a>
        <a onclick="iOrderedList()"><i class='fa fa-list-ol'></i></a>
        <a onclick="iLink()"><i class='fa fa-link'></i></a>
        <a onclick="iUnlink()"><i class='fa fa-chain-broken'></i></a>
        <a onclick="iJustLeft()"><i class='fa fa-align-left'></i></a>
        <a onclick="iJustCenter()"><i class='fa fa-align-center'></i></a>
        <a onclick="iJustRight()"><i class='fa fa-align-right'></i></a>
        <a onclick="iImage()"><i class='fa fa-picture-o'></i></a>
    </div>
    <!-- Hide(but keep) normal textarea and place in the iFrame replacement for it -->
    <textarea style="display:none;" name="myTextArea" id="myTextArea" cols="100" rows="14"></textarea>
    <iframe name="richTextField" id="richTextField"></iframe>
    <!-- End replacing normal textarea -->
    <br />
    <br />
    <button id="article_btn" onclick="saveArticle()">Save Article</button>
    <br />
    <br />
    <br />
</form>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    duan19911992 duan19911992 2017-08-18 08:12

    I tested this and it works fine:

    document.documentElement.innerHTML = '<iframe id="frame"></iframe>';
    var frame = document.getElementById('frame').contentWindow.document;
    frame.open();
    frame.write('<html><body>It Works!</body></html>');
    frame.close();
    

    You can do this also on already rendered iframe.

    点赞 评论 复制链接分享

相关推荐