php - 使用 javascript 在 iframe 中发布 php 脚本

标签 php javascript iframe

我遵循了有关如何制作所见即所得输入文本区域的教程。 文本区域工作正常。我遇到的问题是我希望能够为用户提供编辑文本区域发布的信息的选项。

信息存储在我的数据库中,并查询了数据库并将信息放入名为 $details 的变量中。我想把它放在 iframe 中。

这是我的表单代码。

<body onLoad="iFrameOn();" >
<form action="inventory_edit.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
<div id="wysiwyg_cp" style="padding:8px; width:700px;">
              <input type="button" onClick="iBold()" value="B"> 
              <input type="button" onClick="iUnderline()" value="U">
              <input type="button" onClick="iItalic()" value="I">
              <input type="button" onClick="iFontSize()" value="Text Size">
              <input type="button" onClick="iForeColor()" value="Text Color">
              <input type="button" onClick="iHorizontalRule()" value="HR">
              <input type="button" onClick="iUnorderedList()" value="UL">
              <input type="button" onClick="iOrderedList()" value="OL">
              <input type="button" onClick="iLink()" value="Link">
              <input type="button" onClick="iUnLink()" value="UnLink">
              <input type="button" onClick="iImage()" value="Image">
            </div>
            <!-- Hide(but keep)your normal textarea and place in the iFrame replacement for it -->
            <textarea style="display:none;" name="myTextArea" id="myTextArea" cols="100" rows="14"><?php echo $details; ?> blah blah </textarea>

            <iframe name="richTextField" id="richTextField" style="border:#000000 1px solid; width:700px; height:300px;" ></iframe>



      <input type="submit" name="button" id="button" value="Make Changes" onClick="javascript:submit_form();"/>
</form>

这是 JavaScript 文件

function iFrameOn(){ 
    richTextField.document.designMode = 'On';               
}
function iBold(){
    richTextField.document.execCommand('bold',false,null); 
}
function iUnderline(){
    richTextField.document.execCommand('underline',false,null);
}
function iItalic(){
    richTextField.document.execCommand('italic',false,null); 
}
function iFontSize(){
    var size = prompt('Enter a size 1 - 7', '');
    richTextField.document.execCommand('FontSize',false,size);
}
function iForeColor(){
    var color = prompt('Define a basic color or apply a hexadecimal color code for advanced colors:', '');
    richTextField.document.execCommand('ForeColor',false,color);
}
function iHorizontalRule(){
    richTextField.document.execCommand('inserthorizontalrule',false,null);
}
function iUnorderedList(){
    richTextField.document.execCommand("InsertOrderedList", false,"newOL");
}
function iOrderedList(){
    richTextField.document.execCommand("InsertUnorderedList", false,"newUL");
}
function iLink(){
    var linkURL = prompt("Enter the URL for this link:", "http://"); 
    richTextField.document.execCommand("CreateLink", false, linkURL);
}
function iUnLink(){
    richTextField.document.execCommand("Unlink", false, null);
}
function iImage(){
    var imgSrc = prompt('Enter image location', '');
    if(imgSrc != null){
        richTextField.document.execCommand('insertimage', false, imgSrc); 
    }
}
function submit_form(){
    var theForm = document.getElementById("myform");
    theForm.elements["myTextArea"].value = window.frames['richTextField'].document.body.innerHTML;
    theForm.submit();
}

我花了几个小时试图找到解决办法。这是我尝试将 php var 添加到 iframe 时添加的内容。

这被添加到 iFrameOn() 函数

var theForm = document.getElementById('myform');
window.frame['richTextField'].document.body.innerHTML = theForm.elements['myTextArea'].value;

抱歉,如果代码太多。谢谢

编辑: 这是文本编辑器的样子。 WYSIWYG

我希望能够使用数据库中已有的内容预先填充 iFrame,以便用户可以对其进行编辑。

最佳答案

我使用动态创建的外部文件解决了这个问题,然后调用 iframe。我找不到其他方法来使用 Iframe 来做到这一点。

然而,这有时似乎会给多个用户带来问题 - 幸运的是,我的项目似乎允许我通过创建文件夹和文件来解决这个问题。也许考虑使用 session 变量来创建它(因为它将是唯一的)并在退出时使用 JS 删除文件?

希望有帮助

关于php - 使用 javascript 在 iframe 中发布 php 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11779084/

相关文章:

javascript - AngularJS 中的 SQL IN 语句相当于什么?

javascript - 滚动到顶部并同时刷新页面

jquery - 为什么这个jsfiddle不起作用? (基本的Statelistener youtube iframe视频)

html - 框架?尝试在不离开网络的情况下将动态信息放入网络

php - 添加表单元素后刷新页面给出不同的样式结果

php - WordPress WP_User_Query 搜索

PHP API 最佳实践 - 回显 json_encoded 对象 : bad or good?

请求之间的 PHP cURL multi_exec 延迟

javascript - Google map Pin 点从 Javascript 刷新?

html - 如何删除表格下方的线