简体   繁体   中英

html tags in DB entry not rendering as html

I have a form with a notes section that uses ckeditor so that paragraphs are separated when submitted to a MySQL database. so two paragraphs would submit as follows:

<p>Paragraph 1</p><p>Paragraph 2</p>

My problem is when this data is displayed on a webpage the HTML tags are not being read as HTML as I expected them too. When I view the page in Dev Tools it is showing quote marks outside the first and last HTML tag like below. which indicates the tags are seen as text.

“<p>Paragraph 1</p><p>Paragraph 2</p>”

I am doing the same on another website but this time is working fine and is rendering the HTML tags properly. When I view this in Dev Tools it shows quote marks inside the HTML tags of each paragraph like below. No surprise then the tags are being seen as HTML.

<p>”Paragraph 1”</p><p>”Paragraph 2”</p>

The Difference between the two is the one that displays properly is 'mediumtext' type, and the one that doesn't is 'text' type. Plus the one that doesn't display properly is using the ckeditor plugin.

I am simply using the following to add the ckeditor function:

<textarea rows='4' type="text"  class="notes_c" id="notes_c" 
name="notes_c" value=""></textarea>

<script type="text/javascript" src="../ckeditor/ckeditor.js"></script>
<script>
CKEDITOR.replace('notes_c');
</script>

Any help to understand what is happening here is much appreciated.

Update to this is i've tried using html_entity_decode with the following:

<?php 
$note = $rsCurrentEnquiry->getColumnVal("notes_c"); 
$p = html_entity_decode($note);
?>
<?php echo $p; ?>

Which solves the issue!!

So, I've created a fiddle for you, of what I think you intended.

Notably you should get/set the data from CKeditor with these methods (when getting/setting data dynamically, of course):

CKEDITOR.instances.notes_c.getData();
CKEDITOR.instances.notes_c.setData(data_string);

notes_c - is the id/name/class of the div where CKeditor is.

If have any other question, feel free to ask.

EDIT: the fiddle link was broken, its fixed now. Also added another button to show both getData and setData.

JSFiddle

EDIT2: Sorry, I had indeed misunderstood your question. So, using javascript, I would use AJAX to receive the data from your DB ( <p>Paragraph 1</p><p>Paragraph 2</p> ) and then do something like what I do in this JsFiddle

Do note that by using PHP, if an HTML tag is incorrectly closed, or malformed, before the text you want to insert, then the PHP will append to the badly formed HTML and could interpret your string as only a string, ignoring the HTML tags inside.

I've just had the same problem. I've solved it by manually converting the escape code to the normal characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM