How to preserve formatting of text while storing data in mysql database using php?

你。 提交于 2019-12-21 23:19:07

问题


I'm using HTML5 text editor, using which people can

  1. Make text bold/italics.
  2. Insert link/image.
  3. Change text color, add bullet/numbers etc.

How do I store and retrieve this data in/from mysql database so that the formatting is preserved? Currently, the text is getting stored, but as plain text.

What approach should I adopt to fix this issue?


回答1:


Use htmlentities() function and in MySQL use TEXT as field type of the attribute

let the element has id editor then in js use

$('#editor').html();

Example:

<div id="editor">
  <!--Here goes your content from HTML5 Editor-->
</div>

so use AJAX to save it:

function StoreText() {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if (xmlhttp.responseText == "true") {
                //success message
            } else {
                //error message
            }
        }
    }
    xmlhttp.open("POST", "Update.php", true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.send("value=" + $('#editor').html());
}


来源:https://stackoverflow.com/questions/33389756/how-to-preserve-formatting-of-text-while-storing-data-in-mysql-database-using-ph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!