问题
I'm using HTML5 text editor, using which people can
- Make text bold/italics.
- Insert link/image.
- 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