2019.07.19 Update:
Issue resolved by using
$_REQUEST['editor1']
instead of $_POST['editor1']. Not entirely sure why. Hope it helps to anyone who came across this weird issue.
===========================================
Problem description:
I am integrating CKeditor to my HTML form. Ckeditor is showing on the form successfully, and I am able to enter some data. But when I tried to post the form in order to store in the database, I noticed that the database entry are missing all the HTML open brackets and close brackets. Tried to search on the internet but no luck. Kindly please advise.
My database field is having text type.
I have replaced Ckeditor with TinyMCE, but still the same.
<div class="form-group">
<label for="content"><?=$str['content']?></label>
<div class="input-group mb-3">
<textarea class="ckeditor" cols="80" id="editor1" name="editor1" rows="20"></textarea>
</div>
</div>
<!-- Initializing the editor -->
<script src="//cdn.ckeditor.com/4.12.1/full/ckeditor.js"></script>
<script type="text/javascript">
CKEDITOR.replace( 'editor1' );
</script>
I tried to input the ckeditor textarea with data below:
<h3><a href="https://stackoverflow.com/questions/22904776/insert-ckeditor-html-code-into-the-database">Insert ckeditor html code into the database - Stack Overflow</a></h3>
<p> </p>
<p><a href="https://stackoverflow.com/questions/22904776/insert-ckeditor-html-code-into-the-database"><cite>https://stackoverflow.com/questions/.../insert-ckeditor-html-code-into-the-database</cite></a></p>
But when I stored in database or retrieving the $_POST['editor1'] data, I'm getting this:
h3a href=https://stackoverflow.com/questions/22904776/insert-ckeditor-html-code-into-the-databaseInsert ckeditor html code into the database - Stack Overflow/a/h3 p /p pa href=https://stackoverflow.com/questions/22904776/insert-ckeditor-html-code-into-the-databasecitehttps://stackoverflow.com/questions/.../insert-ckeditor-html-code-into-the-database/cite/a/p
All the open brackets and close brackets are missing. What did I do wrong?
Below is the code to store data:
if (isset($_POST['submit'])) {
$host = DB_HOST; /* Host name */
$user = DB_USER; /* User */
$password = DB_PASS; /* Password */
$dbname = DB_NAME; /* Database name */
$con = mysqli_connect($host, $user, $password, $dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$subject = $_POST['subject'];
$content = $_POST['editor1'];
$publish = $_POST['publish'];
$publish_date = $_POST['publish_date'];
$updated_by = $_SESSION['memberID'];
mysqli_query($con, " INSERT INTO tbl_announcement (subject, content, publish, publish_date, updated_by )
VALUES ( '$subject', '$content', '$publish' , '$publish_date', '$updated_by')");
Check using this : mysqli_real_escape_string() while adding in database
The last time I used the CKEditor, I used to receive the through the form submission. Then looked through the docs and it provides a method to get the data from the editor which you can save through AJAX.
<script>
var data = CKEDITOR.instances.editor1.getData();
// Your code to save "data", usually through Ajax.
</script>
This may solve your problem.
Use html_entity_decode
to convert HTML entities into HTML tags
echo html_entity_decode($_POST['editor1']);
Reference Link here