Im using tinymce with image upload using the package "laravel-tinymce-simple-imageupload". So I have a page where the user can select a registration type and enter some content in the tinymce textarea to associated that content to the selected registration type.
But there is an issue. For example the user can select a registration type and then in the text area write for example "certificate 1" and click in the form "Store" button. In the database the content introduced in the tinymce is stored like:
<p>certificate 1</p>
So this is correct.
But the issue is for example if the user in the text area instead of write just "certificate 1" writes "certificate 1" and then give a enter and in the next line write also for example "test" and click in the form "Store" button the page is refreshed and the tinymce buttons disappear from the textarea and the content introduced in the textarea is inserted in db like:
<p>certificate 1</p>
<p>test</p>
And it appears an error on the console: "Uncaught SyntaxError: Invalid or unexpected token" in:
var certificate = {};
certificate[1] =
'<p>cert1<img src="../../../img/image_1532441196_7.jpeg"
alt="" width="1200" height="900" /></p>';
certificate[2] = '<p>cert2</p>
<p> </p>
<p>cert 2</p>
<p> </p>
<p>aee</p>';
But if i change in the db to "<p>certificate 1</p><p>test</p>
", that is have the content in the same line, the tinymce buttons appear again when the page is refreshed.
Do you know what can be the issue?
Code in certificateController that inserts the certificate content (the content introduced in the tinymce textarea) in the DB:
public function update(Request $request){
$registrationType = RegistrationType::where('id', $request->registrationType)->first();
$certificate = $registrationType->certificate;
// if no certificate exists for this type, create it
if(!$certificate ) {
$certificate = new Certificate();
}
// the certificate_content is the textarea with the tinymce plugin
$certificate->content = $request->certificate_content;
$certificate->save();
$registrationType->certificate_id = $certificate->id;
$registrationType->save();
$certificateContent = RegistrationType::with('certificate')->where('id', $request->registrationType)->first();
Session::flash('success','Certificate configured with success for the selected registration type.');
return redirect()->back();
}
jQuery:
<script type="text/javascript">
var certificate = {};
@foreach($conference->registrationTypes as $registrationType)
@if(!$registrationType->certificate)
certificate[{{ $registrationType->id }}] = '';
@else
certificate[{{ $registrationType->id }}] = '{!! $registrationType->certificate->content !!}';
@endif
@endforeach
$(function () {
$('.radio').change(function () {
var registrationTypeId = $('input[name=registrationType]:checked').val();
$(tinymce.get('certificate_content').getBody()).html(certificate[registrationTypeId]);
});
tinymce.init({
selector: 'textarea',
plugins: 'image code link lists textcolor wordcount ' +
' hr pagebreak colorpicker textpattern anchor table media',
relative_urls: true,
file_browser_callback: function (field_name, url, type, win) {
// trigger file upload form
if (type == 'image') $('#formUpload input').click();
}
});
});
</script>