Search code examples
phphtmlmysqllaraveltinymce

How to get the content of the tinymc text editor so that it can be passed with a form


I'm new with TinyMc and I'm trying to add an editor to my Laravel project. I've been able to get it working so far, but I don't know how to get the content of the editor and pass it to the controller so it can be saved on the database. This is the code of the editor:

<div>
    <form action="{{ route('page.update', ['id' => $text->id]) }}" method="post">

        @csrf
        <textarea id="contenuto">{{$text->Contenuto}}</textarea>
        <label for="mySubmit" class="btn btn-dark"><i class="bi-check-lg"></i> Save</label>
        <input id="mySubmit" type="submit" value="Create" class="hidden" />
        
    </form>
</div>

If I submit this, the section with id="contenuto" results as empty.

I need the form to pass the content of the editor to the controller so it can be saved to the database. The content is simple text with no images

Update

The request gives me an error "Attempt to assign property "Contenuto" on null"

The code of my editor is

<div>
    <form action="{{ route('page.update', ['id' => $text->id]) }}" method="post">

        @csrf
        <div class="form-group">
            <textarea id="contenuto" name="Contenuto">{{$text->Contenuto}}</textarea>
        </div>

        <label for="mySubmit" class="btn btn-dark"><i class="bi-check-lg"></i> Save</label>
        <input id="mySubmit" type="submit" value="Create" class="hidden" />

    </form>
</div>

and the code in the DataLayer.php file is

public function pageStore($id, $contenuto) {
        $dl = new DataLayer();
        $text = $dl -> getTextByID($id);
        $text->Contenuto = $contenuto;
        $text->save();
        // massive update (only with fillable property enabled on Author): 
        // Author::find($id)->update(['firstname' => $first_name, 'lastname' => $last_name]);
    }

    public function getTextByID($id)
    {
        ##$texts = Text::where('text_id', $text_id)->get();
        ##return $texts[0];

        return Text::find($id);
    }


Solution

  • When submitting a form with HTML alone (not javascript), it's the field name attribute that is needed during submission.

    <div>
        <form action="{{ route('page.update', ['id' => $text->id]) }}" method="post">
    
            @csrf
            <textarea id="contenuto" name="contenuto">{{$text->Contenuto}}</textarea>
            <label for="mySubmit" class="btn btn-dark"><i class="bi-check-lg"></i> Save</label>
            <input id="mySubmit" type="submit" value="Create" class="hidden" />
            
        </form>
    </div>