I'm using tinymce plugin on a textarea that accepts user input and insert it into database.
html page:
<textarea name="content" id="content"></textarea>
script to initialize tinymce:
initTinyMCE('#content');
function initTinyMCE(querySelector) {
tinymce.init({
selector:querySelector,
plugins: [
"advlist autolink lists link image charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table contextmenu paste jbimages preview emoticons media",
"textcolor placeholder"
],
toolbar: "bold italic underline strikethrough backcolor emoticons autoresize | bullist numlist outdent indent | insertfile | alignleft aligncenter alignright alignjustify | link media image jbimages undo redo preview",
menubar:false,
paste_data_images: true,
relative_urls: false,
height : "420",
paste_word_valid_elements: "b,strong,i,em,h1,h2,u,p,ol,ul,li,a[href],span,color,mark",
relative_urls : false,
media_dimensions: false,
media_poster: false,
content_css: "/assets/css/bootstrap.min.css",
setup: function (editor) {
editor.on('change', function () {
tinymce.triggerSave();
});
}
});
}
controller:
$data['content'] = trim($this->input->post('content', TRUE));
$this->db->insert('mytable', $data);
the problem is that there's an extra <p> </p>
that gets inserted into the database. since I already trim the variable before inserting into the database, I believe that tinymce automatically adds an empty line to the input. how do I get rid of that extra empty line? do I really have to use regex to delete it manually?
I thought a plugin as widely used and old as tinymce should have a setting or configuration to do that, no?
I am going to assume that you do (in general) want your HTML to have things like <p>
tags you just don't want this "extra" empty paragraph at the end?
If so I suspect that what you are seeing is someone pressing the Enter key to move to a new line but not typing anything on that last line before submitting the form.
When you press enter to move to a new line the editor will create a new "block element" (typically a <p>
) and the cursor goes within this element - then when you type that content goes within that block element.
Trimming the content won't help because the issue is not whitespace but an "empty" paragraph.
If you are specifically looking to trim any empty paragraphs at the end of the content you can do that when the content is submitted and before you save the content but you can't stop someone from pressing enter and then submitting the content before typing into that last block element.