Anil Kumar Sahu
Anil Kumar Sahu

Reputation: 577

How to get Ckeditor textarea value in laravel

i am using Ckeditor for blog posting in my project when i submit the form nothing i am get in controller can any one suggest me solution for that.

my view is looking like

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

my controller code is

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

in previous project ie designed in CI i just use $tc=$this->input->post('tc'); in controller for getting the Ckeditor value but in laravel i am not sure how to get it done.

Upvotes: 0

Views: 11432

Answers (5)

Hassan Elshazly Eida
Hassan Elshazly Eida

Reputation: 839

ckeditor 5

    <div id="editor">This is some sample content.</div>
    <script>
            ClassicEditor
            .create( document.querySelector( '#editor' ) )
            .then( editor => {
                    const data = editor.getData();
                    console.log( data ); // <------
            } )
            .catch( error => {
                    console.error( error );
            } );
    </script>

Upvotes: 0

linktoahref
linktoahref

Reputation: 7972

As mentioned by @user3888958,

<textarea name="tc" id="post_content" rows="10" cols="60" 
    class="span8"  placeholder="Image Title Goes Here" name="post_content">

the textarea has two name attribute.

You could access the textarea content using the name attribute, remove any one name attribute and pass that in as a parameter to the request

$request->input('tc'); // OR
$request->input('post_content');

and to access the value of

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

you could access it using the name

$request->input('p_mdesc');

Upvotes: 0

online Thomas
online Thomas

Reputation: 9381

The documentation has clear examples.

In your Blade you should add ckeditor like this:

<!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>

So the javascript code triggers a replace of a textarea to the editor

Now for the retrieving data part

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

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

You need to create an endpoint if you want to send this data indeed trough Ajax. Don't forget to add a CSRF token

Upvotes: 0

Bhagchandani
Bhagchandani

Reputation: 558

Your view contain 2 name attribute for the post_content field (textarea). Please check.

Upvotes: 2

Nikhil G
Nikhil G

Reputation: 2466

You can do it like this -

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

and then you will have to initialise it

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

Upvotes: 0

Related Questions