CKEDITOR - 在 Laravel 5 中使用 AJAX 更新/创建产品失败

Sai*_*nce 5 php ajax jquery ckeditor laravel-5

我想将 ckeditortextarea字段中编写的 html 内容存储在我products使用 AJAX调用的数据库表中。

一切都被成功插入和/或更新,留下产品描述。

product_edit.blade.php

{!! Form::open(['files' => 'true', 'autocomplete' => 'off', 'name' => 'formEditProductGeneralInfo', 'id' => 'formEditProductGeneralInfo']) !!}
    <div class="form-group">
        {!! Form::label('code', 'Code:') !!}
        {!! Form::text('code', $product->code, ['class' => 'form-control input-sm']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('name', 'Name:') !!}
        {!! Form::text('name', $product->name, ['class' => 'form-control input-sm']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('description', 'Product Description:') !!}
        {!! Form::textarea('description', $product->description, ['class' => 'form-control input-sm ckeditor', 'rows' => 3, 'id' => 'prdDescription']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('price', 'Price:') !!}
        {!! Form::text('price', $product->price, ['class' => 'form-control input-sm']) !!}
    </div>

    <div class="form-group">
       {!! Form::label('image_file', 'Image:') !!}
       {!! Form::file('image_file', ['id' => 'image_file', 'class' => 'form-control input-sm']) !!}
    </div>

    <div class="form-group">
       {!! Form::submit( 'Update', ['class' => 'btn btn-primary btn-block', 'id' => 'btnEditProductGeneral'] ) !!}
    </div>
{!! Form::close() !!}
Run Code Online (Sandbox Code Playgroud)

阿贾克斯

$('form[name="formEditProductGeneralInfo"]').submit(function(e) {
    var inputData = new FormData($(this)[0]);

    var message = CKEDITOR.instances.prdDescription.getData();

    console.log(message);
    console.log(inputData);

    $.ajax({
        url: '{{ url(' / admin / products / update / general ', $product->id) }}',
        type: 'POST',
        data: inputData,
        async: true,
        success: function(m) {
            if (m.status === 'success') {
                toastr.success(m.msg, 'Successs!');
            } else {
                toastr.error(m.msg, msg.status);
            }
        },
        error: function(data) {
            if (data.status === 422) {
                var errors = data.responseJSON;
                var errorsHtml = '<div class="alert alert-danger"><ul>';
                errorsHtml += '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
                $.each(errors, function(key, value) {
                    errorsHtml += '<li>' + value[0] + '</li>';
                });
                errorsHtml += '</ul></div>';
                $('.errors').html(errorsHtml);
            }
        },
        cache: false,
        contentType: false,
        processData: false
    });
    e.preventDefault();
    return false;
});
Run Code Online (Sandbox Code Playgroud)

我该如何将descriptionhtml 内容保存在数据库中?

任何帮助都受到高度赞赏。谢谢。

Sai*_*nce 2

我已经解决了。for loop问题是我必须检查我没有检查过的CKEDITOR 实例。所以我添加了for loop来检查实例,一切都工作得很好。

阿贾克斯更新:

$('form[name="formEditProductGeneralInfo"]').submit(function(e) {
    for (instance in CKEDITOR.instances) {
        CKEDITOR.instances[instance].updateElement();
    }

    var inputData = new FormData($(this)[0]);

    $.ajax({
        url: '{{ url(' / admin / products / update / general ', $product->id) }}',
        type: 'POST',
        data: inputData,
        async: true,
        success: function(m) {
            if (m.status === 'success') {
                toastr.success(m.msg, 'Successs!');
            } else {
                toastr.error(m.msg, msg.status);
            }
        },
        error: function(data) {
            if (data.status === 422) {
                var errors = data.responseJSON;
                var errorsHtml = '<div class="alert alert-danger"><ul>';
                errorsHtml += '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
                $.each(errors, function(key, value) {
                    errorsHtml += '<li>' + value[0] + '</li>';
                });
                errorsHtml += '</ul></div>';
                $('.errors').html(errorsHtml);
            }
        },
        cache: false,
        contentType: false,
        processData: false
    });
    e.preventDefault();
    return false;
});
Run Code Online (Sandbox Code Playgroud)