Retrieving data from mysql database using php, displaying it in WYSIWYG form for editing

I am working on a cms project which is related to the bootstrap WYSIWYG form for inserting and retrieving from the database. The embed code works correctly and the recovery code works well, but it doesn’t work when I want to edit an article. When I click on the edit link, which is <a href='index.php?page=edit&id=".$row['id']."'><span data-placement='top' data-toggle='tooltip' title='Edit'><button class='btn btn-primary btn-xs' data-title='Edit' ><span class='glyphicon glyphicon-pencil'></span></button><span></a> , it links to my edit page. On my edit.php page, I have this code to select from a database that works well

 <?php include("dbconnect.php"); if(isset($_GET['id'])) $id = strip_tags($_GET['id']); $sql = "SELECT * FROM berita WHERE id=$id" ; $result = mysqli_query($conn, $sql); while ($row = mysqli_fetch_assoc($result)) { $image= $row['gambar']; $title = $row['judul']; $description = ( $row['konten']); $time = $row['tanggal']; } ?> 

When I repeat the value in the appropriate form type, it only works well if the Bootstrap-based WYSIWYG does not respond to any value, but if I change it to a regular text field, it works fine. Here is my code on edit.php

 <?php include("dbconnect.php"); if(isset($_GET['id'])) $id = strip_tags($_GET['id']); $sql = "SELECT * FROM berita WHERE id=$id"; $result = mysqli_query($conn, $sql); while ($row = mysqli_fetch_assoc($result)) { $image= $row['gambar']; $title = $row['judul']; $description = ( $row['konten']); $time = $row['tanggal']; } ?> <link href="plugins/WYSIWYG/editor.css" type="text/css" rel="stylesheet"/> <script src="plugins/WYSIWYG/editor.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#txtEditor").Editor(); }); </script> <form name="my_form" action="action.php" method="POST" enctype="multipart/form-data"> <div class="form-group"> <label for="exampleInputEmail1">Date</label> <input type="text" class="form-control" id="time" name="time" value="<?php echo date('dm-Y'); ?>" disabled> <small id="emailHelp" class="form-text text-muted"></small> </div> <div class="form-group"> <label>Article Title</label> <input type="text" class="form-control" id="title" name="title" value="<?php echo $title; ?>" placeholder="title" required /> </div> <div class="form-group"> <label >select categories</label> <select class="form-control" id="cat" name="cat"> <option value="World">World</option> <option value="Sport">Sport</option> <option value="Politics">Politics</option> <option value="Business">Business</option> <option value="Technology">Technology</option> <option value="Entertainment">Entertainment</option> <option value="Fashion">Fashion</option> <option value="Gist">Gist</option> </select> </div> <div class="form-group"> <label>Write Article </label> <textarea class="form-control" id="txtEditor" name="txtEditor"><?php echo htmlspecialchars($description) ;?></textarea> </div> <div class="form-group"> <label for="exampleInputFile">upload image</label> <input type="file" accept="image/*" name="myimage" id="myimage" class="form-control-file" id="exampleInputFile" aria-describedby="fileHelp"> <small id="fileHelp" class="form-text text-muted"></small> </div> <button onclick=" $('#txtEditor').val($('.Editor-editor').html());" type="Publish" id="Publish" name="Publish" class="btn btn-primary">Publish</button> </form> 

Any help please?

+5
source share
5 answers

I found this: p I did a javascript demo here: https://jsfiddle.net/x14vdkk0/1/

If you cannot set the value, then the root cause is in multiple lines. In fact, you are doing something like this:

 $(document).ready(function() { $('#txtEditor').Editor(); $('#txtEditor').Editor('setText', 'My text with <strong>LineControl</strong> :) second line !'); // SECOND LINE IN ERROR ! }); 

You can see (console.log) that the second line part is in error because javascript does not recognize this.

enter image description here

Solution: avoid carriage return

From the database

You have a beautiful json_encode function that will protect you from all problems of this type and javascriptize your variable.

 $(document).ready(function() { $('#txtEditor').Editor(); $('#txtEditor').Editor('setText', <?php echo json_encode($description); ?>); }); 

I did not forget the quotes around the php function. What for? Because the json_encode function will add a quote to your text. If you do json_encode('toto') output will be "toto" ; If you do Exit json_encode([key' => 'toto']) will be {"key":"toto"}

What all:)

Another solution:

Use htmlspecialchars($description) for the text box

 <textarea id="txtEditor"> My text with: &lt;strong&gt;LineControl !&lt;/strong&gt; </textarea> 

and

 $(document).ready(function() { $el = '#txtEditor'; $($el).Editor(); $($el).Editor('setText', $($el).val()); }); 
+1
source

It would be easier if you specify which plugin you use for WYSIWYG in bootstrap. From your code, I realized that this is https://github.com/suyati/line-control

In this case, you need to put the value using the installer, so you must rewrite the declaration code for:

 <script type="text/javascript"> $(document).ready(function () { $("#txtEditor").Editor(); $("#txtEditor").Editor("setText", "<?php echo htmlspecialchars($description) ;?>"); }); </script> 

And remove the php echo from your text box

I don’t understand what you are trying to achieve with the “publish” button, as it seems that you want to replace the content in the editor. It will change the value, but it will not be visible, and it will not submit the form ...

Hope this helps

+1
source
  $(document).ready(function(){ <?php if($description){ ?> $('#txtEditor').Editor("setText", "<?php echo addslashes($description);?>"); <?php } ?> }); 

Link: https://github.com/suyati/line-control/issues/34

+1
source

try repeating the description from textarea and see if anything is displayed. If not, then you do not get the value in your variable.

0
source

I am facing this problem with some other plugin, finally I found that the problem does not occur here:

 <script src="//cdn.ckeditor.com/4.5.11/standard/ckeditor.js"></script> 

and here is my part of the script for loading and storing data in a database: for storing data

 $features = htmlentities($_POST['features'],ENT_QUOTES); 

and for load data, just an echo plug will do the decoding

 <textarea class="form-control" id="features" name="features"><?php echo $data['features']; ?></textarea> 
0
source

Source: https://habr.com/ru/post/1258566/


All Articles