【问题标题】:Why does Ckeditor value returns empty when I disabled standard browser submit with javascript?为什么当我使用 javascript 禁用标准浏览器提交时 Ckeditor 值返回空?
【发布时间】:2018-06-15 03:24:18
【问题描述】:

我有一个使用 ckeditor 的 textarea 字段,但我试图通过 ajax 提交表单并阻止标准浏览器提交,但 ckeditor 返回一个空字段。而且我确实在互联网上搜索过,但似乎没有任何效果。

我的 Html 和 Javscript 代码列在下面提前致谢。

HTML

<script type="text/javascript" src="ckeditor/ckeditor.js"></script>

<form action="processors.php" method="post" enctype="multipart/form-data" 
id="MyUploadForm">
<input type="text" placeholder="Title" name="title">
<input type="file" name="file">
<textarea name="content" id="editor"></textarea>
<button id="submit-btn" class="btn btn-default" type="submit">Submit All</button>
</form>

Javascript

for(var instanceName in CKEDITOR.instances){
CKEDITOR.instances[instanceName].updateElement();
} //To allow ajax in ckeditor

     $('#MyUploadForm').submit(function() {
        $(this).ajaxSubmit(options);
        // always return false to prevent standard browser submit and page navigation
        return false;
    });

var options = {
        target:   '#output',   // target element(s) to be updated with server response
        beforeSubmit:  beforeSubmit,  // pre-submit callback
        success:       afterSuccess,  // post-submit callback
        uploadProgress: OnProgress, //upload progress callback
        resetForm: true        // reset the form after successful submit
    };

    //progress bar function
    function OnProgress(event, position, total, percentComplete)
    {
    //Progress bar
    $('#progressbox').show();
    $('#progressbar1').width(percentComplete + '%') //update progressbar percent complete
    $('#statustxt').html(percentComplete + '%'); //update status text
    if(percentComplete>50)
    {
        $('#statustxt').css('color','#000'); //change status text to white after 50%
    }
 }

//function to format bites bit.ly/19yoIPO
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Bytes';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}

//function to check file size before uploading.
function beforeSubmit(){
//check whether browser fully supports all File API
if (window.File && window.FileReader && window.FileList && window.Blob)
{
    var fsize = $('#FileInput')[0].files[0].size; //get file size
    var ftype = $('#FileInput')[0].files[0].type; // get file type
    //allow file types
    switch(ftype)
    {
  case 'image/png':
        case 'image/gif':
        case 'image/jpeg':
        case 'image/pjpeg':
        case 'text/plain':
        case 'text/html':
        case 'application/x-zip-compressed':
        case 'application/pdf':
        case 'application/msword':
        case 'application/vnd.ms-excel':
        case 'video/mp4':
            break;
        default:

            $("#output").html("<b>"+ftype+"</b> Unsupported file type!");
            return false
    }

    //Allowed file size is less than 5 MB (1048576)
    if(fsize>5242880)
    {
        $("#output").html("<b>"+bytesToSize(fsize) +"</b> Too big file! <br 
    />File is too big, it should be less than 5 MB.");
        return false
    }

    $('#submit-btn').hide(); //hide submit button
    $('#loading-img').show(); //hide submit button
    $("#output").html("");
    }
    else
    {
    //Output error to older unsupported browsers that doesn't support HTML5 
    File API
    $("#output").html("Please upgrade your browser, because your current browser lacks some new features we need!");
    return false;
}
}

每当我尝试使用 php 插入数据库时​​,都会插入除我的 textarea 之外的其他值

【问题讨论】:

  • 我在这里看不到任何显示您实际提交数据的方式。这可能是最重要的部分。
  • 好的,我马上编辑问题
  • 好的 ADyson 我刚刚编辑了问题,请再检查一遍
  • “每当我尝试使用 php 插入数据库时​​,除了我的 textarea 之外,其他值都会被插入”。这并不意味着这些值没有被发布。您是否检查过浏览器的网络选项卡以监控请求并查看请求正文中是否提交了名为“内容”的值以及其他参数?如果是这样,那么问题出在您的 PHP 代码中(我猜您可能没有在寻找正确的变量或其他东西),我们需要看到这一点。
  • 每当我使用普通的 html textarea 时,您都会看到 ADyson,一切正常,但每当我使用 Ckeditor $_POST['content'] 返回一个空值

标签: javascript php ajax ckeditor


【解决方案1】:

我认为你必须搬家

for(var instanceName in CKEDITOR.instances){
  CKEDITOR.instances[instanceName].updateElement();
}

在您的“提交”处理程序中。否则,该代码仅在页面加载时运行。这不好,因为 textarea 只包含加载页面时编辑器包含的任何内容(在您的情况下,这似乎什么都没有)。

相反,它需要在提交表单时运行,以便将编辑器插件的最新内容复制到 textarea 中。

 $('#MyUploadForm').submit(function(event) {
    event.preventDefault(); //this is instead of return false, but they are pretty much interchangeable. I think this is just a bit clearer.

    for(var instanceName in CKEDITOR.instances){
      CKEDITOR.instances[instanceName].updateElement();
    }

    $(this).ajaxSubmit(options);
});

感谢这个答案:How to ajax-submit a form textarea input from CKEditor? 的提示。

【讨论】:

  • @OjoJosephOluwaseun 没问题 - 如果答案对您有所帮助,请记得将其标记为“已接受” - 谢谢 :-)(单击问题旁边的勾号,将其变为绿色,或查看这里:stackoverflow.com/help/someone-answers)
猜你喜欢
  • 2013-10-06
  • 2014-11-26
  • 2019-12-28
  • 1970-01-01
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多