This is my HTML for Blog Content which is using ckeditor.
<div class="row">
<div class="col-12">
<div class="form-group mb-0">
<label class="form-label" for="edit-blog-editor">Blog Content: <span class="validation">*</span></label>
<textarea id="edit-blog-editor" name="edit-blog-editor"></textarea>
</div>
</div>
</div>
Ckeditor is initialised and is working for both add blog modal and edit blog modal.
But While fetching the data using ajax. i am facing an issue. Issue is that i am able to get my content as a response and in console.log as well.
Below is the code for fetching blog data using ajax
function decodeEntities(encodedString) {
var textArea = document.createElement("textarea");
textArea.innerHTML = encodedString;
return textArea.value;
}
function fetchBlogContent(blogId) {
$.ajax({
url: "ajax-blog/fetch_blog_content.php",
type: "POST",
data: { eid: blogId },
dataType: "text",
success: function (content) {
console.log("Received content:", content);
CKEDITOR.instances["edit-blog-editor"].destroy();
$("#edit-blog-editor").val(decodeEntities(content));
CKEDITOR.replace("edit-blog-editor");
},
error: function (error) {
console.log("Error fetching blog content:", error);
},
});
}
console.log("Received content:", content);
this code is returning the blog content data in console.
But After that i am not able to see the data in the text editor.
This is how i am fetching rest of the data
$("#blog-table").on("click", "#edit-blog-button", function () {
let blogId = $(this).data("eid");
$.ajax({
url: "ajax-blog/fetch_blog.php",
type: "POST",
data: { blogId: blogId },
dataType: "json",
success: function (data) {
// Populate modal fields
$("#edit_blog_id").val(blogId);
$("#edit-blog-title").val(data.title);
$("#edit-blog-short-desc").val(data.short_description);
$("#edit-blog-img-alt").val(data.image_alt);
$("#edit-blog-posted-by").val(data.posted_by);
$("#edit-blog-posted-on").val(data.posted_on);
$("#edit-blog-meta-title").val(data.meta_title);
$("#edit-blog-meta-desc").val(data.meta_description);
$("#edit-blog-meta-key").val(data.meta_keywords);
$("#edit-blog-category").val(data.category_slug);
$("#edit-popular-blog").prop("checked", data.popular_blog === "yes");
$("#edit-trending-blog").prop("checked", data.trending_blog === "yes");
$("#edit-top-blog").prop("checked", data.top_blog === "yes");
$("#edit-blog-slug-url").val(data.slug_url);
fetchBlogContent(blogId);
fetchCurrentImage(blogId);
},
error: function (error) {
console.log("Error fetching blog details:", error);
},
});
});
This is how i am fetching blog content using php
<?php
include("../include/config.php");
if (isset($_POST['eid'])) {
$blogId = $_POST['eid'];
$fetch_query = "SELECT blog_content FROM blogs WHERE id=?";
$fetch_stmt = mysqli_prepare($conn, $fetch_query);
if ($fetch_stmt) {
mysqli_stmt_bind_param($fetch_stmt, "i", $blogId);
if (mysqli_stmt_execute($fetch_stmt)) {
$result = mysqli_stmt_get_result($fetch_stmt);
$row = mysqli_fetch_assoc($result);
echo json_encode($row);
} else {
echo json_encode(["error" => "Execution failed"]);
}
} else {
echo json_encode(["error" => "Prepare failed"]);
}
mysqli_stmt_close($fetch_stmt);
mysqli_close($conn);
}
I just want to show the data in ckeditor and i have no idea anymore how to resolve this.
i tried to initialise CK editor again. checked all the response up until putting data in the actual textarea. Which is not working at all.
I just want to fetch my data according to that id in ckeditor.