【问题标题】:How to Add TinyMCE textbox using ajax如何使用 ajax 添加 TinyMCE 文本框
【发布时间】:2013-02-22 13:22:51
【问题描述】:

我希望我的用户从选择列表中选择 templateId,当用户选择然后查询数据库以根据 templateId 带来模板内容。然后我想向 textarea 显示模板内容,但它在 TinyMCE textarea 中显示 html 标签,而不是显示设计(视图)。

它仅在我在页面加载时显示相同的模板内容时才有效,但当我尝试通过从数据库获取相同的内容时它会失败。

希望这一切都能为您提供我所需要的任何线索。

在第 1 步中,代码运行良好...

<textarea id="elm1" name="elm1" rows="15" cols="80" style="width: 50%">
    <?php echo htmlspecialchars($ResultArray[0]['Contents']); ?>
</textarea>

...但是选择的 onchange 值我正在调用选定的模板:

<select name="templateId" id="templateId" onchange="GetTemplateView(this.value);">
    <?php for($i=0; $i<count($ResultArray); $i++){ ?>
    <option value="<?php echo $ResultArray[$i]['TemplateId']; ?>"><?php echo $ResultArray[$i]['Name']; ?></option>
    <?php }?>
</select> 

Ajax 请求是:

function GetTemplateView(templateId)
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {   // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else
    {   // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            if (navigator.appName == 'Microsoft Internet Explorer')
                document.getElementById("templateView").outerHTML = xmlhttp.responseText;
            else
                document.getElementById("templateView").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "js/GetTemplateView.php?id=" + templateId + "&encode=true", true);
    xmlhttp.send();
}

GetTemplateView.php:

<?php
include_once("../classes/db_utility_class.php");
$ClassObj= new Cdb();
$input        =   $_GET["id"];
$Encode       =   $_GET["encode"];

if($input!=0)
{
    $query = "SELECT Contents FROM mailtemplate WHERE TemplateId=".$input;
    $result = $ClassObj->getRowFromDB($query);
    if($result != "Error in query" || $result != "No Record Found")
    {
        if($Encode)
        {
            echo '<textarea id="ele1" name="ele1" rows="15" cols="80" style="width: 50%">';
            echo htmlspecialchars($result['Contents']);
            echo '</textarea>';
        }
        else
            echo $result['Contents'];
    }
}
?> 

【问题讨论】:

    标签: php javascript ajax tinymce


    【解决方案1】:

    先用jquery.ajax比较好用。

    并且 1. 将 tinyMCE 存储到 processpage.php 中的变量中。

    1. 并在您的 ajax 调用的成功函数中将其附加到您希望它显示的任何 div 或正文中。

    查看示例代码。在需要时调用它。

    <script>
                 $.ajax({
                    url: processpage.php, 
                    type: 'GET',                     
                    contentType: 'text/html',
                    dataType: 'text',
                    async: false,
                    data:{
                                        showTINYMCE : 'Yes'
                                    },
                    success: function(msg)
                    {
                        $('body').append(msg);
                    },
                    error: function (msg) 
                    {
                    }
                });
    
    </script>
    

    在你的 processpage.php 中:

    if($_GET['showTINYMCE'] == 'Yes')
    {
    $tinymce = "contains the tinymce";     
    echo $tinymce;
    }
    

    这样就可以了。

    【讨论】:

    • 如何将选择控件的值发送到 processpage.php
    • 您能否再解释一下您对“选择控件值”的看法。那只有我能给你答案了
    猜你喜欢
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多