簡體   English   中英

如何使用Ajax添加TinyMCE文本框

[英]How to Add TinyMCE textbox using ajax

我希望我的用戶從選擇列表中選擇templateId,當用戶選擇然后查詢數據庫以基於templateId帶來模板內容時。 然后,我想將模板內容顯示為textarea,但它在TinyMCE textarea中顯示html標簽,而不是Design(視圖)。

它僅在頁面加載時顯示相同的templateContents時有效,而當我嘗試通過從數據庫獲取顯示相同的內容時,它將失敗。

希望所有這些都能為您提供我所需要的任何線索。

在步驟1中,代碼運行正常...

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

...但是select的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'];
    }
}
?> 

首先使用jquery.ajax更容易使用。

並且1.將tinyMCE存儲到processpage.php中的變量中。

  1. 然后在ajax調用的成功函數中,將其追加到要顯示的任何div或body上。

查看示例代碼。根據需要調用它。

<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;
}

這會做到的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM