Check out the following code snippet:
function add_post() {
if (document.getElementById("blogTitle").value.trim() == "") {
alert("Don't forget to insert a Blog Title!");
return;
}
if(CKEDITOR.instances.editor1.getData() == "") {
alert("Please include some details in your blog post");
return;
}
var data = new FormData(document.getElementById("file_add"));
var xhr = new XMLHttpRequest();
var de=CKEDITOR.instances.editor1.getData();
xhr.open("POST", "upload.php", false)
xhr.send(data + "&de=" + de);
if (xhr.status == 200) {
alert(xhr.responseText);
if(xhr.responseText == 2) {
document.getElementById("res").innerHTML="Remember to Upload The File";
}
if(xhr.responseText == 1) {
document.getElementById("res").innerHTML="Your Blog has been Created!";
document.getElementById("blogTitle").value="";
document.getElementById("editor1").value="";
}
}
else {
output.innerHTML += "An error with status " + xhr.status + " occurred during file upload.<br />";
}
}
I am utilizing CKEDITOR within a textarea to capture formatted text input. By using
CKEDITOR.instances.editor1.getData()
, I can retrieve the value entered into the textarea. Additionally, I chose to use FormData for uploading forms and files via AJAX. While attempting to send the data, there were difficulties in successfully transmitting it. To address this issue, I have stored the textarea value in the variable de
.