0

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>&nbsp;</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')");
  • please upload your php code as well for better understanding. – danish-khan-I Jul 18 '19 at 06:10
  • use `htmlentity()` to convert brackets to entities and on your view use `html_entity_decode();` to convert back to html code.. – danish-khan-I Jul 18 '19 at 06:13
  • I am simply saving $field['myDBTextField'] = $_POST['editor1']; In the database, the open and close brackets are missing. Even if I tried to print_r($_POST) The $_POST['editor1'] is also showing me entry without open and close brackets – Wicked App Jul 18 '19 at 06:45
  • The video is here: https://streamable.com/n44em I have tried using serialize($_POST['editor1]) and htmlentities(_$POST['editor1']), both no luck – Wicked App Jul 18 '19 at 06:58
  • the link is broken.. – danish-khan-I Jul 18 '19 at 07:02

3 Answers3

0

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.

Ropali Munshi
  • 2,757
  • 4
  • 22
  • 45
  • Thanks for your suggestion, but I am submitting the form directly. Wonder which part went wrong. – Wicked App Jul 18 '19 at 06:43
  • @Wicked App That's what I am trying to tell you that it does not behave as expected when you submit yours form directly. – Ropali Munshi Jul 18 '19 at 06:50
  • I tried to adopt this method by adding another hidden field called annContent. On submit button clicked, I assigned $('#annContent').val(CKEDITOR.instances.editor1.getData() ) I can see that value of annContent is having the complete html tag and brackets. But still when I submit the form through jquery, the DB still not saving the open and close brackets – Wicked App Jul 18 '19 at 07:36
  • I can clearly see from the developer console that after posting, the editor1 / annContent value are having all the open and close brackets. But when store in the DB, all the brackets are disappeared. – Wicked App Jul 18 '19 at 08:22
  • @Wicked App Show your code how you are storing the data. – Ropali Munshi Jul 18 '19 at 08:44
0

Use html_entity_decode to convert HTML entities into HTML tags

echo html_entity_decode($_POST['editor1']);

Reference Link here

ManiMuthuPandi
  • 1,594
  • 2
  • 26
  • 46
-1

Check using this : mysqli_real_escape_string() while adding in database