如何在laravel中获得Ckeditor textarea值

时间:2017-08-08 07:51:17

标签: laravel fckeditor

我正在使用Ckeditor在我的项目中发布博客,当我提交表单时,我得到的控制器没有任何人建议我解决此问题。

我的观点看起来像

<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Post</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ route('store-post') }}">
                        {{ csrf_field() }}

                            <div class="form-group">
                              <label for="category_id" class="col-md-2 control-label">Select Categories</label>
                               <div class="col-md-8">
                              <select class="form-control" id="category_id" name="category_id">


                                @foreach($categories as $category)
                                       <option value="{{$category->url_name}}">
                                       {{$category->category_name}}</option>
                                @endforeach

                              </select>
                              </div>
                            </div>


                        <div class="form-group">
                            <label for="email" class="col-md-2 control-label">Post Title</label>

                            <div class="col-md-8">
                                <input id="post_title" type="text" class="form-control" name="post_title" value="{{ old('post_title') }}">
                            </div>
                        </div>

                          <div class="form-group">
                            <label for="post_content" class="col-md-2 control-label">Post Description</label>

                            <div class="col-md-8">
                             <textarea id="post_content" rows="10" cols="60" class="span8"  placeholder="Image Title Goes Here" name="post_content"></textarea>

                             </div>
                        </div>



                        <div class="form-group">
                            <label for="p_url" class="col-md-2 control-label">Post Url</label>

                            <div class="col-md-8">
                                <input id="p_url" type="text" class="form-control" name="p_url" value="{{ old('p_url') }}">
                            </div>
                        </div>



                        <div class="form-group">
                            <label for="p_title" class="col-md-2 control-label">Meta Title</label>

                            <div class="col-md-8">
                                <input id="p_title" type="text" class="form-control" name="p_title" value="{{ old('p_title') }}">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="p_keyword" class="col-md-2 control-label">Meta Keyword</label>

                            <div class="col-md-8">
                                <input id="p_keyword" type="text" class="form-control" name="p_keyword" value="{{ old('p_keyword') }}">
                            </div>
                        </div>


                        <div class="form-group">
                            <label for="email" class="col-md-2 control-label">Meta Description</label>

                            <div class="col-md-8">

                            <textarea class="form-control" id="p_mdesc" name="p_mdesc" rows="3">

                            </textarea>

                            </div>
                        </div>






                        <div class="form-group">

                            <div class="col-md-8 col-md-offset-2">
                                <button type="submit" class="btn btn-primary">
                                    Submit
                                </button>


                            </div>
                        </div>

                            <!--Error start-->
                             @if ($errors->any())
                                <div class="alert alert-danger">
                                    <ul>
                                        @foreach ($errors->all() as $error)
                                            <li>{{ $error }}</li>
                                        @endforeach
                                    </ul>
                                </div>
                            @endif
                            <!--error ends-->



                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

我的控制器代码是

public function store(Request $request){
        /*$this->validate($request, [
                        'category_id' => 'required',
                        'post_title' => 'required',
                        //'post_content' => 'required',
                        'p_url' => 'required',
                        'p_title' => 'required',
                        'p_keyword' => 'required',
                        'p_mdesc' => 'required',
                    ]);*/

        $post=new Post;
        echo $post_content=$request->input('post_content');
    }

在以前的项目中,即在CI中设计我只是使用 控制器中的$tc=$this->input->post('tc');用于获取Ckeditor值但在laravel中我不知道如何完成它。

3 个答案:

答案 0 :(得分:2)

您的视图包含post_content字段(textarea)的2个name属性。请检查。

答案 1 :(得分:0)

你可以这样做 -

{!! Form::textarea('tc', $tc,array('required', 'class'=>'form-control', placeholder'=>'Your message')) !!}

然后你必须初始化它

$(document).ready(function () {
     CKEDITOR.replace( 'tc' );
  });

答案 2 :(得分:0)

文档中有明显的例子。

在你的Blade中,你应该像这样添加ckeditor:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>A Simple Page with CKEditor</title>
        <!-- Make sure the path to CKEditor is correct. -->
        <script src="../ckeditor.js"></script>
    </head>
    <body>
        <form>
            <textarea name="editor1" id="editor1" rows="10" cols="80">
                This is my textarea to be replaced with CKEditor.
            </textarea>
            <script>
                // Replace the <textarea id="editor1"> with a CKEditor
                // instance, using default configuration.
                CKEDITOR.replace( 'editor1' );
            </script>
        </form>
    </body>
</html>

因此,javascript代码会触发将textarea替换为编辑器

现在检索数据部分

<script>
    var data = CKEDITOR.instances.editor1.getData();

    // Your code to save "data", usually through Ajax.
</script>

如果要通过Ajax发送此数据,则需要创建端点。别忘了添加CSRF令牌